style: cargo fmt with the workspace tab policy (hard_tabs)

Whitespace only; the fork has no own rustfmt.toml so the Oak root
policy applies.
This commit is contained in:
2026-08-11 23:06:13 +08:00
parent 507d7e6e8d
commit af5482d774
318 changed files with 132564 additions and 132170 deletions
+244 -236
View File
@@ -6,8 +6,8 @@
//! apply it through its engine instead.
use gpui::{
App, Bounds, Context, Entity, Render, Window, WindowBounds, WindowOptions,
colors::DefaultColors, div, prelude::*, px, size,
App, Bounds, Context, Entity, Render, Window, WindowBounds, WindowOptions,
colors::DefaultColors, div, prelude::*, px, size,
};
use gpui_widgets::checkbox::{CheckBox, CheckBoxEvent, CheckState};
use gpui_widgets::color::{ColorPicker, ColorPickerEvent};
@@ -20,262 +20,270 @@ use gpui_widgets::spinbox::{SpinBox, SpinBoxEvent};
use gpui_widgets::value::ValueKind;
struct Example {
exposure: Entity<Slider>,
frame_rate: Entity<Slider>,
shutter_angle: Entity<Slider>,
iso: Entity<SpinBox>,
format: Entity<ComboBox>,
muted: Entity<CheckBox>,
solo: Entity<CheckBox>,
playback_mode: Entity<RadioGroup>,
color: Entity<ColorPicker>,
remap: Entity<CurveEditor>,
exposure: Entity<Slider>,
frame_rate: Entity<Slider>,
shutter_angle: Entity<Slider>,
iso: Entity<SpinBox>,
format: Entity<ComboBox>,
muted: Entity<CheckBox>,
solo: Entity<CheckBox>,
playback_mode: Entity<RadioGroup>,
color: Entity<ColorPicker>,
remap: Entity<CurveEditor>,
}
impl Example {
fn new(window: &mut Window, cx: &mut Context<Self>) -> Self {
// Float slider with a key at the current frame.
let exposure = cx.new(|cx| {
Slider::new(
1,
SliderModel::new(ValueKind::Float, -5.0, 5.0, 0.1, 0.0),
window,
cx,
)
.with_keying(KeyingState::AtCurrentFrame)
});
cx.subscribe(
&exposure,
|_this: &mut Self, _s: Entity<Slider>, event: &SliderEvent, _cx| {
println!("exposure request: {event:?}");
},
)
.detach();
fn new(window: &mut Window, cx: &mut Context<Self>) -> Self {
// Float slider with a key at the current frame.
let exposure = cx.new(|cx| {
Slider::new(
1,
SliderModel::new(ValueKind::Float, -5.0, 5.0, 0.1, 0.0),
window,
cx,
)
.with_keying(KeyingState::AtCurrentFrame)
});
cx.subscribe(
&exposure,
|_this: &mut Self, _s: Entity<Slider>, event: &SliderEvent, _cx| {
println!("exposure request: {event:?}");
},
)
.detach();
// Rational slider: 1/24 .. 24/24 in numerator steps.
let frame_rate = cx.new(|cx| {
Slider::new(
2,
SliderModel::new(ValueKind::Rational, 1.0, 24.0, 1.0, 24.0)
.with_rational_den(24),
window,
cx,
)
.with_keying(KeyingState::HasKey)
});
cx.subscribe(
&frame_rate,
|_this: &mut Self, _s: Entity<Slider>, event: &SliderEvent, _cx| {
println!("frame rate request: {event:?}");
},
)
.detach();
// Rational slider: 1/24 .. 24/24 in numerator steps.
let frame_rate = cx.new(|cx| {
Slider::new(
2,
SliderModel::new(ValueKind::Rational, 1.0, 24.0, 1.0, 24.0).with_rational_den(24),
window,
cx,
)
.with_keying(KeyingState::HasKey)
});
cx.subscribe(
&frame_rate,
|_this: &mut Self, _s: Entity<Slider>, event: &SliderEvent, _cx| {
println!("frame rate request: {event:?}");
},
)
.detach();
// Angle slider (degrees).
let shutter_angle = cx.new(|cx| {
Slider::new(
3,
SliderModel::new(ValueKind::Angle, 0.0, 360.0, 1.0, 180.0),
window,
cx,
)
.with_keying(KeyingState::NoKey)
});
cx.subscribe(
&shutter_angle,
|_this: &mut Self, _s: Entity<Slider>, event: &SliderEvent, _cx| {
println!("shutter request: {event:?}");
},
)
.detach();
// Angle slider (degrees).
let shutter_angle = cx.new(|cx| {
Slider::new(
3,
SliderModel::new(ValueKind::Angle, 0.0, 360.0, 1.0, 180.0),
window,
cx,
)
.with_keying(KeyingState::NoKey)
});
cx.subscribe(
&shutter_angle,
|_this: &mut Self, _s: Entity<Slider>, event: &SliderEvent, _cx| {
println!("shutter request: {event:?}");
},
)
.detach();
let iso = cx.new(|cx| {
SpinBox::new(
4,
SliderModel::new(ValueKind::Integer, 100.0, 12800.0, 100.0, 800.0),
window,
cx,
)
});
cx.subscribe(
&iso,
|_this: &mut Self, _s: Entity<SpinBox>, event: &SpinBoxEvent, _cx| {
println!("iso request: {event:?}");
},
)
.detach();
let iso = cx.new(|cx| {
SpinBox::new(
4,
SliderModel::new(ValueKind::Integer, 100.0, 12800.0, 100.0, 800.0),
window,
cx,
)
});
cx.subscribe(
&iso,
|_this: &mut Self, _s: Entity<SpinBox>, event: &SpinBoxEvent, _cx| {
println!("iso request: {event:?}");
},
)
.detach();
let format = cx.new(|cx| {
ComboBox::new(
5,
vec![
ComboBoxOption::new(1, "Frame"),
ComboBoxOption::new(2, "Timecode"),
ComboBoxOption::new(3, "Frames"),
],
window,
cx,
)
.with_placeholder("Choose…")
});
cx.subscribe(
&format,
|_this: &mut Self, _s: Entity<ComboBox>, event: &ComboBoxEvent, _cx| {
println!("format request: {event:?}");
},
)
.detach();
let format = cx.new(|cx| {
ComboBox::new(
5,
vec![
ComboBoxOption::new(1, "Frame"),
ComboBoxOption::new(2, "Timecode"),
ComboBoxOption::new(3, "Frames"),
],
window,
cx,
)
.with_placeholder("Choose…")
});
cx.subscribe(
&format,
|_this: &mut Self, _s: Entity<ComboBox>, event: &ComboBoxEvent, _cx| {
println!("format request: {event:?}");
},
)
.detach();
let muted = cx.new(|cx| {
CheckBox::new(6, CheckState::Unchecked, window, cx).with_label("Mute")
});
cx.subscribe(
&muted,
|_this: &mut Self, _s: Entity<CheckBox>, event: &CheckBoxEvent, _cx| {
println!("mute request: {event:?}");
},
)
.detach();
let muted =
cx.new(|cx| CheckBox::new(6, CheckState::Unchecked, window, cx).with_label("Mute"));
cx.subscribe(
&muted,
|_this: &mut Self, _s: Entity<CheckBox>, event: &CheckBoxEvent, _cx| {
println!("mute request: {event:?}");
},
)
.detach();
let solo = cx.new(|cx| {
CheckBox::new(7, CheckState::Indeterminate, window, cx)
.with_label("Solo")
.with_tri_state(true)
});
cx.subscribe(
&solo,
|_this: &mut Self, _s: Entity<CheckBox>, event: &CheckBoxEvent, _cx| {
println!("solo request: {event:?}");
},
)
.detach();
let solo = cx.new(|cx| {
CheckBox::new(7, CheckState::Indeterminate, window, cx)
.with_label("Solo")
.with_tri_state(true)
});
cx.subscribe(
&solo,
|_this: &mut Self, _s: Entity<CheckBox>, event: &CheckBoxEvent, _cx| {
println!("solo request: {event:?}");
},
)
.detach();
let playback_mode = cx.new(|cx| {
RadioGroup::new(
8,
vec![
RadioOption::new(1, "Loop"),
RadioOption::new(2, "Ping-pong"),
RadioOption::new(3, "Once"),
],
window,
cx,
)
});
cx.subscribe(
&playback_mode,
|_this: &mut Self, _s: Entity<RadioGroup>, event: &RadioGroupEvent, _cx| {
println!("playback mode request: {event:?}");
},
)
.detach();
let playback_mode = cx.new(|cx| {
RadioGroup::new(
8,
vec![
RadioOption::new(1, "Loop"),
RadioOption::new(2, "Ping-pong"),
RadioOption::new(3, "Once"),
],
window,
cx,
)
});
cx.subscribe(
&playback_mode,
|_this: &mut Self, _s: Entity<RadioGroup>, event: &RadioGroupEvent, _cx| {
println!("playback mode request: {event:?}");
},
)
.detach();
let color = cx.new(|cx| {
ColorPicker::new(9, gpui::Rgba { r: 1.0, g: 0.4, b: 0.1, a: 1.0 }, window, cx)
});
cx.subscribe(
&color,
|_this: &mut Self, _s: Entity<ColorPicker>, event: &ColorPickerEvent, _cx| {
println!("color request: {event:?}");
},
)
.detach();
let color = cx.new(|cx| {
ColorPicker::new(
9,
gpui::Rgba {
r: 1.0,
g: 0.4,
b: 0.1,
a: 1.0,
},
window,
cx,
)
});
cx.subscribe(
&color,
|_this: &mut Self, _s: Entity<ColorPicker>, event: &ColorPickerEvent, _cx| {
println!("color request: {event:?}");
},
)
.detach();
let remap = cx.new(|cx| {
CurveEditor::new(
10,
vec![
CurvePoint::with_handles(0.0, 0.0, CurveVec2::new(0.0, 0.5)),
CurvePoint::with_handles(1.0, 1.0, CurveVec2::new(0.0, -0.5)),
],
window,
cx,
)
});
cx.subscribe(
&remap,
|_this: &mut Self, _s: Entity<CurveEditor>, event: &CurveEditorEvent, _cx| {
println!("remap request: {event:?}");
},
)
.detach();
let remap = cx.new(|cx| {
CurveEditor::new(
10,
vec![
CurvePoint::with_handles(0.0, 0.0, CurveVec2::new(0.0, 0.5)),
CurvePoint::with_handles(1.0, 1.0, CurveVec2::new(0.0, -0.5)),
],
window,
cx,
)
});
cx.subscribe(
&remap,
|_this: &mut Self, _s: Entity<CurveEditor>, event: &CurveEditorEvent, _cx| {
println!("remap request: {event:?}");
},
)
.detach();
Self {
exposure,
frame_rate,
shutter_angle,
iso,
format,
muted,
solo,
playback_mode,
color,
remap,
}
}
Self {
exposure,
frame_rate,
shutter_angle,
iso,
format,
muted,
solo,
playback_mode,
color,
remap,
}
}
}
impl Render for Example {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let colors = cx.default_colors().clone();
div()
.size_full()
.bg(colors.background)
.flex()
.flex_col()
.p_4()
.gap_3()
.id("example-scroll")
.overflow_y_scroll()
.child(section("Exposure (float)", self.exposure.clone()))
.child(section("Frame rate (rational)", self.frame_rate.clone()))
.child(section("Shutter (angle)", self.shutter_angle.clone()))
.child(section("ISO (spinbox)", self.iso.clone()))
.child(section("Time format (combo)", self.format.clone()))
.child(section("Audio", self.muted.clone()))
.child(section("Track", self.solo.clone()))
.child(section("Playback mode", self.playback_mode.clone()))
.child(section("Accent color", self.color.clone()))
.child(
div()
.flex()
.flex_col()
.gap_1()
.child(div().text_color(colors.text).child("Time remap (curve)"))
.child(self.remap.clone()),
)
}
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let colors = cx.default_colors().clone();
div()
.size_full()
.bg(colors.background)
.flex()
.flex_col()
.p_4()
.gap_3()
.id("example-scroll")
.overflow_y_scroll()
.child(section("Exposure (float)", self.exposure.clone()))
.child(section("Frame rate (rational)", self.frame_rate.clone()))
.child(section("Shutter (angle)", self.shutter_angle.clone()))
.child(section("ISO (spinbox)", self.iso.clone()))
.child(section("Time format (combo)", self.format.clone()))
.child(section("Audio", self.muted.clone()))
.child(section("Track", self.solo.clone()))
.child(section("Playback mode", self.playback_mode.clone()))
.child(section("Accent color", self.color.clone()))
.child(
div()
.flex()
.flex_col()
.gap_1()
.child(div().text_color(colors.text).child("Time remap (curve)"))
.child(self.remap.clone()),
)
}
}
/// A labeled row used by the demo panel.
fn section(label: impl Into<gpui::SharedString>, widget: impl IntoElement) -> impl IntoElement {
div()
.flex()
.items_center()
.gap_3()
.child(div().w(px(130.0)).child(label.into()))
.child(widget)
div()
.flex()
.items_center()
.gap_3()
.child(div().w(px(130.0)).child(label.into()))
.child(widget)
}
fn main() {
gpui_platform::application().run(|cx: &mut App| {
cx.init_colors();
let bounds = Bounds::centered(None, size(px(620.0), px(760.0)), cx);
cx.open_window(
WindowOptions {
window_bounds: Some(WindowBounds::Windowed(bounds)),
..Default::default()
},
|window, cx| cx.new(|cx| Example::new(window, cx)),
)
.expect("Failed to open window");
gpui_platform::application().run(|cx: &mut App| {
cx.init_colors();
let bounds = Bounds::centered(None, size(px(620.0), px(760.0)), cx);
cx.open_window(
WindowOptions {
window_bounds: Some(WindowBounds::Windowed(bounds)),
..Default::default()
},
|window, cx| cx.new(|cx| Example::new(window, cx)),
)
.expect("Failed to open window");
cx.activate(true);
cx.on_window_closed(|cx, _| {
if cx.windows().is_empty() {
cx.quit();
}
})
.detach();
});
cx.activate(true);
cx.on_window_closed(|cx, _| {
if cx.windows().is_empty() {
cx.quit();
}
})
.detach();
});
}
+241 -238
View File
@@ -3,266 +3,269 @@
//! printed as a request event.
use gpui::{
App, Bounds, Context, Entity, Focusable, MouseButton, MouseDownEvent, Render, Window,
WindowBounds, WindowOptions, colors::DefaultColors, div, prelude::*, px, size,
App, Bounds, Context, Entity, Focusable, MouseButton, MouseDownEvent, Render, Window,
WindowBounds, WindowOptions, colors::DefaultColors, div, prelude::*, px, size,
};
use gpui_widgets::dialog::file_dialog::{FileDialogContent, file_dialog};
use gpui_widgets::dialog::message_box::{MessageBoxLevel, message_box};
use gpui_widgets::dialog::progress::{ProgressContent, progress_dialog};
use gpui_widgets::dialog::{Modal, ModalEvent};
use gpui_widgets::menu::{ContextMenu, ContextMenuEvent, Menu, MenuBar, MenuBarEntry, MenuBarEvent, MenuItem};
use gpui_widgets::menu::{
ContextMenu, ContextMenuEvent, Menu, MenuBar, MenuBarEntry, MenuBarEvent, MenuItem,
};
struct Example {
menu_bar: Entity<MenuBar>,
context_menu: Entity<ContextMenu>,
message: Entity<Modal>,
progress: Entity<Modal>,
progress_content: Entity<ProgressContent>,
file: Entity<Modal>,
file_content: Entity<FileDialogContent>,
show_message: bool,
show_progress: bool,
show_file: bool,
menu_bar: Entity<MenuBar>,
context_menu: Entity<ContextMenu>,
message: Entity<Modal>,
progress: Entity<Modal>,
progress_content: Entity<ProgressContent>,
file: Entity<Modal>,
file_content: Entity<FileDialogContent>,
show_message: bool,
show_progress: bool,
show_file: bool,
}
impl Example {
fn new(window: &mut Window, cx: &mut Context<Self>) -> Self {
let menu_bar = cx.new(|cx| {
MenuBar::new(
1,
vec![
MenuBarEntry::new(
"File",
Menu::new(vec![
MenuItem::new(10, "New…").with_shortcut("⌘N"),
MenuItem::new(11, "Open…").with_shortcut("⌘O"),
MenuItem::new(12, "Save").with_shortcut("⌘S").separated(),
MenuItem::new(13, "Export").disabled(),
]),
),
MenuBarEntry::new(
"Edit",
Menu::new(vec![
MenuItem::new(20, "Undo").with_shortcut("⌘Z"),
MenuItem::new(21, "Redo").with_shortcut("⇧⌘Z").separated(),
MenuItem::new(22, "Show Diagnostics").with_checked(false),
]),
),
MenuBarEntry::new(
"View",
Menu::new(vec![
MenuItem::new(30, "Toolbars"),
MenuItem::new(31, "Scopes"),
MenuItem::new(32, "Theme").with_submenu(Menu::new(vec![
MenuItem::new(33, "Olive Dark").with_checked(true),
MenuItem::new(34, "Olive Light"),
])),
]),
),
],
window,
cx,
)
});
cx.subscribe(
&menu_bar,
|_this: &mut Self, _m: Entity<MenuBar>, event: &MenuBarEvent, _cx| {
println!("menu bar: {event:?}");
},
)
.detach();
fn new(window: &mut Window, cx: &mut Context<Self>) -> Self {
let menu_bar = cx.new(|cx| {
MenuBar::new(
1,
vec![
MenuBarEntry::new(
"File",
Menu::new(vec![
MenuItem::new(10, "New…").with_shortcut("⌘N"),
MenuItem::new(11, "Open…").with_shortcut("⌘O"),
MenuItem::new(12, "Save").with_shortcut("⌘S").separated(),
MenuItem::new(13, "Export").disabled(),
]),
),
MenuBarEntry::new(
"Edit",
Menu::new(vec![
MenuItem::new(20, "Undo").with_shortcut("⌘Z"),
MenuItem::new(21, "Redo").with_shortcut("⇧⌘Z").separated(),
MenuItem::new(22, "Show Diagnostics").with_checked(false),
]),
),
MenuBarEntry::new(
"View",
Menu::new(vec![
MenuItem::new(30, "Toolbars"),
MenuItem::new(31, "Scopes"),
MenuItem::new(32, "Theme").with_submenu(Menu::new(vec![
MenuItem::new(33, "Olive Dark").with_checked(true),
MenuItem::new(34, "Olive Light"),
])),
]),
),
],
window,
cx,
)
});
cx.subscribe(
&menu_bar,
|_this: &mut Self, _m: Entity<MenuBar>, event: &MenuBarEvent, _cx| {
println!("menu bar: {event:?}");
},
)
.detach();
let context_menu = cx.new(|cx| ContextMenu::new(2, window, cx));
cx.subscribe(
&context_menu,
|_this: &mut Self, _m: Entity<ContextMenu>, event: &ContextMenuEvent, _cx| {
println!("context menu: {event:?}");
},
)
.detach();
let context_menu = cx.new(|cx| ContextMenu::new(2, window, cx));
cx.subscribe(
&context_menu,
|_this: &mut Self, _m: Entity<ContextMenu>, event: &ContextMenuEvent, _cx| {
println!("context menu: {event:?}");
},
)
.detach();
let message = message_box(
3,
MessageBoxLevel::Warning,
"Unsaved changes",
"Your project has unsaved changes. Export anyway?",
window,
cx,
);
cx.subscribe(
&message,
|this: &mut Self, _m: Entity<Modal>, event: &ModalEvent, _cx| {
println!("message box: {event:?}");
if matches!(event, ModalEvent::ButtonClicked { .. }) {
this.show_message = false;
}
},
)
.detach();
let message = message_box(
3,
MessageBoxLevel::Warning,
"Unsaved changes",
"Your project has unsaved changes. Export anyway?",
window,
cx,
);
cx.subscribe(
&message,
|this: &mut Self, _m: Entity<Modal>, event: &ModalEvent, _cx| {
println!("message box: {event:?}");
if matches!(event, ModalEvent::ButtonClicked { .. }) {
this.show_message = false;
}
},
)
.detach();
let (progress, progress_content) = progress_dialog(4, "Exporting…", "Encoding video", window, cx);
cx.subscribe(
&progress,
|this: &mut Self, _m: Entity<Modal>, event: &ModalEvent, _cx| {
println!("progress: {event:?}");
if matches!(event, ModalEvent::ButtonClicked { .. }) {
this.show_progress = false;
}
},
)
.detach();
let (progress, progress_content) =
progress_dialog(4, "Exporting…", "Encoding video", window, cx);
cx.subscribe(
&progress,
|this: &mut Self, _m: Entity<Modal>, event: &ModalEvent, _cx| {
println!("progress: {event:?}");
if matches!(event, ModalEvent::ButtonClicked { .. }) {
this.show_progress = false;
}
},
)
.detach();
let (file, file_content) = file_dialog(5, "Open media…", window, cx);
cx.subscribe(
&file,
|this: &mut Self, _m: Entity<Modal>, event: &ModalEvent, _cx| {
if let ModalEvent::ButtonClicked { button: 0, .. } = event {
let path = this.file_content.read(_cx).path(_cx);
println!("open file: {path}");
}
println!("file dialog: {event:?}");
if matches!(event, ModalEvent::ButtonClicked { .. }) {
this.show_file = false;
}
},
)
.detach();
let (file, file_content) = file_dialog(5, "Open media…", window, cx);
cx.subscribe(
&file,
|this: &mut Self, _m: Entity<Modal>, event: &ModalEvent, _cx| {
if let ModalEvent::ButtonClicked { button: 0, .. } = event {
let path = this.file_content.read(_cx).path(_cx);
println!("open file: {path}");
}
println!("file dialog: {event:?}");
if matches!(event, ModalEvent::ButtonClicked { .. }) {
this.show_file = false;
}
},
)
.detach();
Self {
menu_bar,
context_menu,
message,
progress,
progress_content,
file,
file_content,
show_message: false,
show_progress: false,
show_file: false,
}
}
Self {
menu_bar,
context_menu,
message,
progress,
progress_content,
file,
file_content,
show_message: false,
show_progress: false,
show_file: false,
}
}
}
impl Render for Example {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let colors = cx.default_colors().clone();
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let colors = cx.default_colors().clone();
let mut root = div()
.size_full()
.bg(colors.background)
.flex()
.flex_col()
.child(self.menu_bar.clone())
.child(
div()
.flex_1()
.flex()
.items_center()
.justify_center()
.gap_2()
.text_color(colors.text)
.id("example-main")
.on_mouse_down(
MouseButton::Right,
cx.listener(|this, event: &MouseDownEvent, _window, cx| {
this.context_menu.update(cx, |menu, cx| {
menu.show(
event.position,
Menu::new(vec![
MenuItem::new(40, "Cut").with_shortcut("⌘X"),
MenuItem::new(41, "Copy").with_shortcut("⌘C"),
MenuItem::new(42, "Paste").with_shortcut("⌘V"),
MenuItem::new(43, "Delete").disabled().separated(),
MenuItem::new(44, "Properties…"),
]),
cx,
);
});
}),
)
.child(div().child("Right-click for a context menu"))
.child(
div()
.id("example-btn-message")
.px_3()
.py_1()
.rounded_md()
.bg(colors.selected)
.text_color(colors.selected_text)
.cursor_pointer()
.on_click(cx.listener(|this, _event: &gpui::ClickEvent, window, cx| {
this.show_message = true;
window.focus(&this.message.read(cx).focus_handle(cx), cx);
}))
.child("Message box"),
)
.child(
div()
.id("example-btn-progress")
.px_3()
.py_1()
.rounded_md()
.bg(colors.selected)
.text_color(colors.selected_text)
.cursor_pointer()
.on_click(cx.listener(|this, _event: &gpui::ClickEvent, window, cx| {
this.show_progress = true;
this.progress_content
.update(cx, |content, cx| content.set_progress(0.4, cx));
window.focus(&this.progress.read(cx).focus_handle(cx), cx);
}))
.child("Progress"),
)
.child(
div()
.id("example-btn-file")
.px_3()
.py_1()
.rounded_md()
.bg(colors.selected)
.text_color(colors.selected_text)
.cursor_pointer()
.on_click(cx.listener(|this, _event: &gpui::ClickEvent, window, cx| {
this.show_file = true;
window.focus(&this.file.read(cx).focus_handle(cx), cx);
}))
.child("Open file…"),
),
)
.child(self.context_menu.clone());
let mut root = div()
.size_full()
.bg(colors.background)
.flex()
.flex_col()
.child(self.menu_bar.clone())
.child(
div()
.flex_1()
.flex()
.items_center()
.justify_center()
.gap_2()
.text_color(colors.text)
.id("example-main")
.on_mouse_down(
MouseButton::Right,
cx.listener(|this, event: &MouseDownEvent, _window, cx| {
this.context_menu.update(cx, |menu, cx| {
menu.show(
event.position,
Menu::new(vec![
MenuItem::new(40, "Cut").with_shortcut("⌘X"),
MenuItem::new(41, "Copy").with_shortcut("⌘C"),
MenuItem::new(42, "Paste").with_shortcut("⌘V"),
MenuItem::new(43, "Delete").disabled().separated(),
MenuItem::new(44, "Properties…"),
]),
cx,
);
});
}),
)
.child(div().child("Right-click for a context menu"))
.child(
div()
.id("example-btn-message")
.px_3()
.py_1()
.rounded_md()
.bg(colors.selected)
.text_color(colors.selected_text)
.cursor_pointer()
.on_click(cx.listener(|this, _event: &gpui::ClickEvent, window, cx| {
this.show_message = true;
window.focus(&this.message.read(cx).focus_handle(cx), cx);
}))
.child("Message box"),
)
.child(
div()
.id("example-btn-progress")
.px_3()
.py_1()
.rounded_md()
.bg(colors.selected)
.text_color(colors.selected_text)
.cursor_pointer()
.on_click(cx.listener(|this, _event: &gpui::ClickEvent, window, cx| {
this.show_progress = true;
this.progress_content
.update(cx, |content, cx| content.set_progress(0.4, cx));
window.focus(&this.progress.read(cx).focus_handle(cx), cx);
}))
.child("Progress"),
)
.child(
div()
.id("example-btn-file")
.px_3()
.py_1()
.rounded_md()
.bg(colors.selected)
.text_color(colors.selected_text)
.cursor_pointer()
.on_click(cx.listener(|this, _event: &gpui::ClickEvent, window, cx| {
this.show_file = true;
window.focus(&this.file.read(cx).focus_handle(cx), cx);
}))
.child("Open file…"),
),
)
.child(self.context_menu.clone());
if self.show_message {
root = root.child(self.message.clone());
}
if self.show_progress {
root = root.child(self.progress.clone());
}
if self.show_file {
root = root.child(self.file.clone());
}
root
}
if self.show_message {
root = root.child(self.message.clone());
}
if self.show_progress {
root = root.child(self.progress.clone());
}
if self.show_file {
root = root.child(self.file.clone());
}
root
}
}
fn main() {
gpui_platform::application().run(|cx: &mut App| {
cx.init_colors();
let bounds = Bounds::centered(None, size(px(640.0), px(480.0)), cx);
cx.open_window(
WindowOptions {
window_bounds: Some(WindowBounds::Windowed(bounds)),
..Default::default()
},
|window, cx| cx.new(|cx| Example::new(window, cx)),
)
.expect("Failed to open window");
gpui_platform::application().run(|cx: &mut App| {
cx.init_colors();
let bounds = Bounds::centered(None, size(px(640.0), px(480.0)), cx);
cx.open_window(
WindowOptions {
window_bounds: Some(WindowBounds::Windowed(bounds)),
..Default::default()
},
|window, cx| cx.new(|cx| Example::new(window, cx)),
)
.expect("Failed to open window");
cx.activate(true);
cx.on_window_closed(|cx, _| {
if cx.windows().is_empty() {
cx.quit();
}
})
.detach();
});
cx.activate(true);
cx.on_window_closed(|cx, _| {
if cx.windows().is_empty() {
cx.quit();
}
})
.detach();
});
}
@@ -3,106 +3,106 @@
//! drop).
use gpui::{
App, Bounds, Context, Entity, Render, Window, WindowBounds, WindowOptions, div, prelude::*,
px, size,
App, Bounds, Context, Entity, Render, Window, WindowBounds, WindowOptions, div, prelude::*, px,
size,
};
use gpui_widgets::project_explorer::{
ProjectDataSource, ProjectEntry, ProjectExplorer, ProjectExplorerEvent,
ProjectDataSource, ProjectEntry, ProjectExplorer, ProjectExplorerEvent,
};
struct MockProject {
entries: Vec<ProjectEntry>,
entries: Vec<ProjectEntry>,
}
impl MockProject {
fn demo() -> Self {
Self {
entries: vec![
ProjectEntry::new(1, "Footage", true),
ProjectEntry::new(2, "Bins", true),
ProjectEntry::new(3, "Notes.md", false),
],
}
}
fn demo() -> Self {
Self {
entries: vec![
ProjectEntry::new(1, "Footage", true),
ProjectEntry::new(2, "Bins", true),
ProjectEntry::new(3, "Notes.md", false),
],
}
}
}
impl ProjectDataSource for MockProject {
fn roots(&self) -> Vec<ProjectEntry> {
self.entries
.iter()
.filter(|e| matches!(e.id, 1..=3))
.cloned()
.collect()
}
fn roots(&self) -> Vec<ProjectEntry> {
self.entries
.iter()
.filter(|e| matches!(e.id, 1..=3))
.cloned()
.collect()
}
fn children(&self, parent_id: u64) -> Vec<ProjectEntry> {
match parent_id {
1 => vec![
ProjectEntry::new(10, "intro.mov", false).with_thumbnail("assets/thumb.png"),
ProjectEntry::new(11, "b-roll.mov", false),
ProjectEntry::new(12, "interview.mov", false),
],
2 => vec![
ProjectEntry::new(20, "Selects", true),
ProjectEntry::new(21, "Music", true),
],
20 => vec![ProjectEntry::new(200, "best-take.mov", false)],
21 => vec![
ProjectEntry::new(210, "track-01.wav", false),
ProjectEntry::new(211, "track-02.wav", false),
],
_ => Vec::new(),
}
}
fn children(&self, parent_id: u64) -> Vec<ProjectEntry> {
match parent_id {
1 => vec![
ProjectEntry::new(10, "intro.mov", false).with_thumbnail("assets/thumb.png"),
ProjectEntry::new(11, "b-roll.mov", false),
ProjectEntry::new(12, "interview.mov", false),
],
2 => vec![
ProjectEntry::new(20, "Selects", true),
ProjectEntry::new(21, "Music", true),
],
20 => vec![ProjectEntry::new(200, "best-take.mov", false)],
21 => vec![
ProjectEntry::new(210, "track-01.wav", false),
ProjectEntry::new(211, "track-02.wav", false),
],
_ => Vec::new(),
}
}
}
struct Example {
explorer: Entity<ProjectExplorer<MockProject>>,
explorer: Entity<ProjectExplorer<MockProject>>,
}
impl Example {
fn new(window: &mut Window, cx: &mut Context<Self>) -> Self {
let project = cx.new(|_| MockProject::demo());
let explorer = cx.new(|cx| ProjectExplorer::new(1, project, window, cx));
cx.subscribe(
&explorer,
|_this: &mut Self,
_e: Entity<ProjectExplorer<MockProject>>,
event: &ProjectExplorerEvent,
_cx| {
println!("explorer request: {event:?}");
},
)
.detach();
Self { explorer }
}
fn new(window: &mut Window, cx: &mut Context<Self>) -> Self {
let project = cx.new(|_| MockProject::demo());
let explorer = cx.new(|cx| ProjectExplorer::new(1, project, window, cx));
cx.subscribe(
&explorer,
|_this: &mut Self,
_e: Entity<ProjectExplorer<MockProject>>,
event: &ProjectExplorerEvent,
_cx| {
println!("explorer request: {event:?}");
},
)
.detach();
Self { explorer }
}
}
impl Render for Example {
fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
div().size_full().child(self.explorer.clone())
}
fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
div().size_full().child(self.explorer.clone())
}
}
fn main() {
gpui_platform::application().run(|cx: &mut App| {
cx.init_colors();
let bounds = Bounds::centered(None, size(px(420.0), px(560.0)), cx);
cx.open_window(
WindowOptions {
window_bounds: Some(WindowBounds::Windowed(bounds)),
..Default::default()
},
|window, cx| cx.new(|cx| Example::new(window, cx)),
)
.expect("Failed to open window");
gpui_platform::application().run(|cx: &mut App| {
cx.init_colors();
let bounds = Bounds::centered(None, size(px(420.0), px(560.0)), cx);
cx.open_window(
WindowOptions {
window_bounds: Some(WindowBounds::Windowed(bounds)),
..Default::default()
},
|window, cx| cx.new(|cx| Example::new(window, cx)),
)
.expect("Failed to open window");
cx.activate(true);
cx.on_window_closed(|cx, _| {
if cx.windows().is_empty() {
cx.quit();
}
})
.detach();
});
cx.activate(true);
cx.on_window_closed(|cx, _| {
if cx.windows().is_empty() {
cx.quit();
}
})
.detach();
});
}
+110 -112
View File
@@ -5,150 +5,148 @@
//! Run with `cargo run -p gpui_widgets --example scopes`.
use gpui::{
App, Bounds, Context, Entity, Render, Window, WindowBounds, WindowOptions, div, prelude::*,
px, size,
App, Bounds, Context, Entity, Render, Window, WindowBounds, WindowOptions, div, prelude::*, px,
size,
};
use gpui_widgets::audio_meter::{AudioLevelMeter, AudioMeterDataSource};
use gpui_widgets::scopes::{ChromaDataSource, Histogram, LumaDataSource, Vectorscope, Waveform};
struct MockLuma {
frame: u64,
frame: u64,
}
impl LumaDataSource for MockLuma {
fn luma_samples(&self) -> Vec<f32> {
// A moving gradient + noise-ish bars.
(0..4096)
.map(|i| {
let x = i as f32 / 4096.0;
let t = self.frame as f32 / 60.0;
((x + t * 0.25).fract() * 0.8 + 0.1).clamp(0.0, 1.0)
})
.collect()
}
fn luma_samples(&self) -> Vec<f32> {
// A moving gradient + noise-ish bars.
(0..4096)
.map(|i| {
let x = i as f32 / 4096.0;
let t = self.frame as f32 / 60.0;
((x + t * 0.25).fract() * 0.8 + 0.1).clamp(0.0, 1.0)
})
.collect()
}
}
struct MockChroma {
frame: u64,
frame: u64,
}
impl ChromaDataSource for MockChroma {
fn chroma_samples(&self) -> Vec<(f32, f32)> {
// A rotating ring in chroma space.
let t = self.frame as f32 / 60.0;
(0..2048)
.map(|i| {
let angle = i as f32 / 2048.0 * std::f32::consts::TAU;
(
0.5 + 0.4 * (angle + t).cos(),
0.5 + 0.4 * (angle + t).sin(),
)
})
.collect()
}
fn chroma_samples(&self) -> Vec<(f32, f32)> {
// A rotating ring in chroma space.
let t = self.frame as f32 / 60.0;
(0..2048)
.map(|i| {
let angle = i as f32 / 2048.0 * std::f32::consts::TAU;
(0.5 + 0.4 * (angle + t).cos(), 0.5 + 0.4 * (angle + t).sin())
})
.collect()
}
}
struct MockAudio {
frame: u64,
frame: u64,
}
impl AudioMeterDataSource for MockAudio {
fn levels(&self) -> Vec<f32> {
let t = self.frame as f32 / 60.0;
vec![
(0.5 + 0.5 * (t * 2.0).sin()).clamp(0.0, 1.0),
(0.5 + 0.5 * (t * 1.7).cos()).clamp(0.0, 1.0),
]
}
fn levels(&self) -> Vec<f32> {
let t = self.frame as f32 / 60.0;
vec![
(0.5 + 0.5 * (t * 2.0).sin()).clamp(0.0, 1.0),
(0.5 + 0.5 * (t * 1.7).cos()).clamp(0.0, 1.0),
]
}
}
struct Example {
luma: Entity<MockLuma>,
chroma: Entity<MockChroma>,
audio: Entity<MockAudio>,
histogram: Entity<Histogram<MockLuma>>,
waveform: Entity<Waveform<MockLuma>>,
vectorscope: Entity<Vectorscope<MockChroma>>,
meter: Entity<AudioLevelMeter<MockAudio>>,
luma: Entity<MockLuma>,
chroma: Entity<MockChroma>,
audio: Entity<MockAudio>,
histogram: Entity<Histogram<MockLuma>>,
waveform: Entity<Waveform<MockLuma>>,
vectorscope: Entity<Vectorscope<MockChroma>>,
meter: Entity<AudioLevelMeter<MockAudio>>,
}
impl Example {
fn new(window: &mut Window, cx: &mut Context<Self>) -> Self {
let luma = cx.new(|_| MockLuma { frame: 0 });
let chroma = cx.new(|_| MockChroma { frame: 0 });
let audio = cx.new(|_| MockAudio { frame: 0 });
fn new(window: &mut Window, cx: &mut Context<Self>) -> Self {
let luma = cx.new(|_| MockLuma { frame: 0 });
let chroma = cx.new(|_| MockChroma { frame: 0 });
let audio = cx.new(|_| MockAudio { frame: 0 });
let histogram = cx.new(|cx| Histogram::new(1, luma.clone(), window, cx));
let waveform = cx.new(|cx| Waveform::new(2, luma.clone(), window, cx));
let vectorscope = cx.new(|cx| Vectorscope::new(3, chroma.clone(), window, cx));
let meter = cx.new(|cx| AudioLevelMeter::new(4, audio.clone(), window, cx));
let histogram = cx.new(|cx| Histogram::new(1, luma.clone(), window, cx));
let waveform = cx.new(|cx| Waveform::new(2, luma.clone(), window, cx));
let vectorscope = cx.new(|cx| Vectorscope::new(3, chroma.clone(), window, cx));
let meter = cx.new(|cx| AudioLevelMeter::new(4, audio.clone(), window, cx));
let this = cx.weak_entity();
window.spawn(cx, async move |cx: &mut gpui::AsyncWindowContext| {
loop {
cx.background_executor()
.timer(std::time::Duration::from_millis(16))
.await;
let _ = cx.update(|_window, app| {
if let Some(this) = this.upgrade() {
this.update(app, |this, cx| this.tick(cx));
}
});
}
})
.detach();
let this = cx.weak_entity();
window
.spawn(cx, async move |cx: &mut gpui::AsyncWindowContext| {
loop {
cx.background_executor()
.timer(std::time::Duration::from_millis(16))
.await;
let _ = cx.update(|_window, app| {
if let Some(this) = this.upgrade() {
this.update(app, |this, cx| this.tick(cx));
}
});
}
})
.detach();
Self {
luma,
chroma,
audio,
histogram,
waveform,
vectorscope,
meter,
}
}
Self {
luma,
chroma,
audio,
histogram,
waveform,
vectorscope,
meter,
}
}
fn tick(&mut self, cx: &mut Context<Self>) {
self.luma.update(cx, |luma, _| luma.frame += 1);
self.chroma.update(cx, |chroma, _| chroma.frame += 1);
self.audio.update(cx, |audio, _| audio.frame += 1);
self.meter.update(cx, |meter, cx| meter.update(cx));
cx.notify();
}
fn tick(&mut self, cx: &mut Context<Self>) {
self.luma.update(cx, |luma, _| luma.frame += 1);
self.chroma.update(cx, |chroma, _| chroma.frame += 1);
self.audio.update(cx, |audio, _| audio.frame += 1);
self.meter.update(cx, |meter, cx| meter.update(cx));
cx.notify();
}
}
impl Render for Example {
fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
div()
.size_full()
.flex()
.flex_col()
.gap_2()
.p_2()
.child(div().h(px(120.0)).child(self.histogram.clone()))
.child(div().h(px(120.0)).child(self.waveform.clone()))
.child(div().h(px(140.0)).child(self.vectorscope.clone()))
.child(div().h(px(60.0)).child(self.meter.clone()))
}
fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
div()
.size_full()
.flex()
.flex_col()
.gap_2()
.p_2()
.child(div().h(px(120.0)).child(self.histogram.clone()))
.child(div().h(px(120.0)).child(self.waveform.clone()))
.child(div().h(px(140.0)).child(self.vectorscope.clone()))
.child(div().h(px(60.0)).child(self.meter.clone()))
}
}
fn main() {
gpui_platform::application().run(|cx: &mut App| {
cx.init_colors();
let bounds = Bounds::centered(None, size(px(520.0), px(560.0)), cx);
cx.open_window(
WindowOptions {
window_bounds: Some(WindowBounds::Windowed(bounds)),
..Default::default()
},
|window, cx| cx.new(|cx| Example::new(window, cx)),
)
.expect("Failed to open window");
gpui_platform::application().run(|cx: &mut App| {
cx.init_colors();
let bounds = Bounds::centered(None, size(px(520.0), px(560.0)), cx);
cx.open_window(
WindowOptions {
window_bounds: Some(WindowBounds::Windowed(bounds)),
..Default::default()
},
|window, cx| cx.new(|cx| Example::new(window, cx)),
)
.expect("Failed to open window");
cx.activate(true);
cx.on_window_closed(|cx, _| {
if cx.windows().is_empty() {
cx.quit();
}
})
.detach();
});
cx.activate(true);
cx.on_window_closed(|cx, _| {
if cx.windows().is_empty() {
cx.quit();
}
})
.detach();
});
}
+105 -99
View File
@@ -2,8 +2,8 @@
//! widget that reads `cx.default_colors()` re-themes immediately.
use gpui::{
App, Bounds, ClickEvent, Context, Entity, Render, Window, WindowBounds, WindowOptions,
colors::DefaultColors, div, prelude::*, px, size,
App, Bounds, ClickEvent, Context, Entity, Render, Window, WindowBounds, WindowOptions,
colors::DefaultColors, div, prelude::*, px, size,
};
use gpui_widgets::checkbox::{CheckBox, CheckBoxEvent, CheckState};
use gpui_widgets::slider::{Slider, SliderEvent, SliderModel};
@@ -11,114 +11,120 @@ use gpui_widgets::theme::{OakTheme, apply_theme};
use gpui_widgets::value::ValueKind;
struct Example {
slider: Entity<Slider>,
checkbox: Entity<CheckBox>,
dark: bool,
slider: Entity<Slider>,
checkbox: Entity<CheckBox>,
dark: bool,
}
impl Example {
fn new(window: &mut Window, cx: &mut Context<Self>) -> Self {
let slider = cx.new(|cx| {
Slider::new(
1,
SliderModel::new(ValueKind::Float, 0.0, 1.0, 0.05, 0.5),
window,
cx,
)
});
cx.subscribe(
&slider,
|_this: &mut Self, _s: Entity<Slider>, event: &SliderEvent, _cx| {
println!("slider: {event:?}");
},
)
.detach();
fn new(window: &mut Window, cx: &mut Context<Self>) -> Self {
let slider = cx.new(|cx| {
Slider::new(
1,
SliderModel::new(ValueKind::Float, 0.0, 1.0, 0.05, 0.5),
window,
cx,
)
});
cx.subscribe(
&slider,
|_this: &mut Self, _s: Entity<Slider>, event: &SliderEvent, _cx| {
println!("slider: {event:?}");
},
)
.detach();
let checkbox = cx.new(|cx| {
CheckBox::new(2, CheckState::Checked, window, cx).with_label("Track enabled")
});
cx.subscribe(
&checkbox,
|_this: &mut Self, _c: Entity<CheckBox>, event: &CheckBoxEvent, _cx| {
println!("checkbox: {event:?}");
},
)
.detach();
let checkbox = cx.new(|cx| {
CheckBox::new(2, CheckState::Checked, window, cx).with_label("Track enabled")
});
cx.subscribe(
&checkbox,
|_this: &mut Self, _c: Entity<CheckBox>, event: &CheckBoxEvent, _cx| {
println!("checkbox: {event:?}");
},
)
.detach();
Self {
slider,
checkbox,
dark: true,
}
}
Self {
slider,
checkbox,
dark: true,
}
}
}
impl Render for Example {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let colors = cx.default_colors().clone();
let theme = if self.dark {
OakTheme::olive_dark()
} else {
OakTheme::olive_light()
};
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let colors = cx.default_colors().clone();
let theme = if self.dark {
OakTheme::olive_dark()
} else {
OakTheme::olive_light()
};
div()
.size_full()
.bg(theme.base)
.flex()
.flex_col()
.gap_3()
.p_6()
.child(
div()
.text_color(colors.text)
.child(format!("Current theme: {}", theme.name)),
)
.child(div().flex().items_center().gap_2().child(self.slider.clone()))
.child(self.checkbox.clone())
.child(
div()
.id("theme-toggle")
.px_3()
.py_1()
.rounded_md()
.bg(colors.selected)
.text_color(colors.selected_text)
.cursor_pointer()
.on_click(cx.listener(|this, _event: &ClickEvent, _window, cx| {
this.dark = !this.dark;
let theme = if this.dark {
OakTheme::olive_dark()
} else {
OakTheme::olive_light()
};
apply_theme(cx, &theme);
cx.notify();
}))
.child("Toggle theme"),
)
}
div()
.size_full()
.bg(theme.base)
.flex()
.flex_col()
.gap_3()
.p_6()
.child(
div()
.text_color(colors.text)
.child(format!("Current theme: {}", theme.name)),
)
.child(
div()
.flex()
.items_center()
.gap_2()
.child(self.slider.clone()),
)
.child(self.checkbox.clone())
.child(
div()
.id("theme-toggle")
.px_3()
.py_1()
.rounded_md()
.bg(colors.selected)
.text_color(colors.selected_text)
.cursor_pointer()
.on_click(cx.listener(|this, _event: &ClickEvent, _window, cx| {
this.dark = !this.dark;
let theme = if this.dark {
OakTheme::olive_dark()
} else {
OakTheme::olive_light()
};
apply_theme(cx, &theme);
cx.notify();
}))
.child("Toggle theme"),
)
}
}
fn main() {
gpui_platform::application().run(|cx: &mut App| {
apply_theme(cx, &OakTheme::olive_dark());
let bounds = Bounds::centered(None, size(px(480.0), px(320.0)), cx);
cx.open_window(
WindowOptions {
window_bounds: Some(WindowBounds::Windowed(bounds)),
..Default::default()
},
|window, cx| cx.new(|cx| Example::new(window, cx)),
)
.expect("Failed to open window");
gpui_platform::application().run(|cx: &mut App| {
apply_theme(cx, &OakTheme::olive_dark());
let bounds = Bounds::centered(None, size(px(480.0), px(320.0)), cx);
cx.open_window(
WindowOptions {
window_bounds: Some(WindowBounds::Windowed(bounds)),
..Default::default()
},
|window, cx| cx.new(|cx| Example::new(window, cx)),
)
.expect("Failed to open window");
cx.activate(true);
cx.on_window_closed(|cx, _| {
if cx.windows().is_empty() {
cx.quit();
}
})
.detach();
});
cx.activate(true);
cx.on_window_closed(|cx, _| {
if cx.windows().is_empty() {
cx.quit();
}
})
.detach();
});
}
+150 -148
View File
@@ -3,184 +3,186 @@
//!
//! Run with `cargo run -p gpui_widgets --example viewer`.
use gpui::{
App, Bounds, Context, Entity, Render, Window, WindowBounds, WindowOptions, div, prelude::*,
px, size,
};
use gpui::timeline::{Frame, FrameRate};
use gpui::{
App, Bounds, Context, Entity, Render, Window, WindowBounds, WindowOptions, div, prelude::*, px,
size,
};
use gpui_widgets::viewer::{PlaybackClock, ViewerEvent, ViewerWidget};
#[cfg(target_os = "macos")]
mod frame_gen {
use core_video::pixel_buffer::CVPixelBuffer;
use std::ffi::c_void;
use core_video::pixel_buffer::CVPixelBuffer;
use std::ffi::c_void;
const WIDTH: u32 = 1280;
const HEIGHT: u32 = 720;
const WIDTH: u32 = 1280;
const HEIGHT: u32 = 720;
/// Generates cheap test frames (a moving hue band) into CVPixelBuffers.
pub struct FrameGen {
frame: u64,
}
/// Generates cheap test frames (a moving hue band) into CVPixelBuffers.
pub struct FrameGen {
frame: u64,
}
impl FrameGen {
pub fn new() -> Self {
Self { frame: 0 }
}
impl FrameGen {
pub fn new() -> Self {
Self { frame: 0 }
}
pub fn next(&mut self) -> CVPixelBuffer {
let mut bytes = vec![0u8; (WIDTH * HEIGHT * 4) as usize];
for y in 0..HEIGHT {
for x in 0..WIDTH {
let i = ((y * WIDTH + x) * 4) as usize;
let t = self.frame as f32 / 60.0;
let band = if (x as f32 / 128.0 + t * 8.0).fract() < 0.5 {
1.0
} else {
0.35
};
bytes[i] = (255.0f32 * band).round() as u8;
bytes[i + 1] = (255.0f32 * band * (y as f32 / HEIGHT as f32)).round() as u8;
bytes[i + 2] = (255.0f32 * band * (1.0 - y as f32 / HEIGHT as f32)).round() as u8;
bytes[i + 3] = 255;
}
}
let callback_ref: Box<Vec<u8>> = Box::new(bytes);
let release_con = Box::into_raw(callback_ref) as *mut c_void;
self.frame += 1;
unsafe {
CVPixelBuffer::new_with_bytes(
0x42475241, // kCVPixelFormatType_32BGRA
WIDTH as usize,
HEIGHT as usize,
release_con as *mut c_void,
(WIDTH * 4) as usize,
free_bytes,
release_con,
None,
)
}
.expect("failed to create test pixel buffer")
}
pub fn next(&mut self) -> CVPixelBuffer {
let mut bytes = vec![0u8; (WIDTH * HEIGHT * 4) as usize];
for y in 0..HEIGHT {
for x in 0..WIDTH {
let i = ((y * WIDTH + x) * 4) as usize;
let t = self.frame as f32 / 60.0;
let band = if (x as f32 / 128.0 + t * 8.0).fract() < 0.5 {
1.0
} else {
0.35
};
bytes[i] = (255.0f32 * band).round() as u8;
bytes[i + 1] = (255.0f32 * band * (y as f32 / HEIGHT as f32)).round() as u8;
bytes[i + 2] =
(255.0f32 * band * (1.0 - y as f32 / HEIGHT as f32)).round() as u8;
bytes[i + 3] = 255;
}
}
let callback_ref: Box<Vec<u8>> = Box::new(bytes);
let release_con = Box::into_raw(callback_ref) as *mut c_void;
self.frame += 1;
unsafe {
CVPixelBuffer::new_with_bytes(
0x42475241, // kCVPixelFormatType_32BGRA
WIDTH as usize,
HEIGHT as usize,
release_con as *mut c_void,
(WIDTH * 4) as usize,
free_bytes,
release_con,
None,
)
}
.expect("failed to create test pixel buffer")
}
}
}
extern "C" fn free_bytes(release_ref_con: *mut c_void, _base: *const *const c_void) {
if !release_ref_con.is_null() {
unsafe {
drop(Box::from_raw(release_ref_con as *mut Vec<u8>));
}
}
}
extern "C" fn free_bytes(release_ref_con: *mut c_void, _base: *const *const c_void) {
if !release_ref_con.is_null() {
unsafe {
drop(Box::from_raw(release_ref_con as *mut Vec<u8>));
}
}
}
}
struct MockClock {
frame: Frame,
playing: bool,
rate: FrameRate,
frame: Frame,
playing: bool,
rate: FrameRate,
}
impl PlaybackClock for MockClock {
fn current_frame(&self) -> Frame {
self.frame
}
fn is_playing(&self) -> bool {
self.playing
}
fn frame_rate(&self) -> FrameRate {
self.rate
}
fn current_frame(&self) -> Frame {
self.frame
}
fn is_playing(&self) -> bool {
self.playing
}
fn frame_rate(&self) -> FrameRate {
self.rate
}
}
struct Example {
clock: Entity<MockClock>,
viewer: Entity<ViewerWidget<MockClock>>,
#[cfg(target_os = "macos")]
frame_gen: frame_gen::FrameGen,
clock: Entity<MockClock>,
viewer: Entity<ViewerWidget<MockClock>>,
#[cfg(target_os = "macos")]
frame_gen: frame_gen::FrameGen,
}
impl Example {
fn new(window: &mut Window, cx: &mut Context<Self>) -> Self {
let clock = cx.new(|_| MockClock {
frame: Frame(0),
playing: true,
rate: FrameRate::new(30, 1),
});
let viewer = cx.new(|cx| ViewerWidget::new(1, clock.clone(), window, cx));
cx.subscribe(
&viewer,
|_this: &mut Self, _v: Entity<ViewerWidget<MockClock>>, event: &ViewerEvent, _cx| {
println!("viewer request: {event:?}");
},
)
.detach();
fn new(window: &mut Window, cx: &mut Context<Self>) -> Self {
let clock = cx.new(|_| MockClock {
frame: Frame(0),
playing: true,
rate: FrameRate::new(30, 1),
});
let viewer = cx.new(|cx| ViewerWidget::new(1, clock.clone(), window, cx));
cx.subscribe(
&viewer,
|_this: &mut Self, _v: Entity<ViewerWidget<MockClock>>, event: &ViewerEvent, _cx| {
println!("viewer request: {event:?}");
},
)
.detach();
let this = cx.weak_entity();
window.spawn(cx, async move |cx: &mut gpui::AsyncWindowContext| {
loop {
cx.background_executor()
.timer(std::time::Duration::from_millis(16))
.await;
let _ = cx.update(|_window, app| {
if let Some(this) = this.upgrade() {
this.update(app, |this, cx| this.tick(cx));
}
});
}
})
.detach();
let this = cx.weak_entity();
window
.spawn(cx, async move |cx: &mut gpui::AsyncWindowContext| {
loop {
cx.background_executor()
.timer(std::time::Duration::from_millis(16))
.await;
let _ = cx.update(|_window, app| {
if let Some(this) = this.upgrade() {
this.update(app, |this, cx| this.tick(cx));
}
});
}
})
.detach();
Self {
clock,
viewer,
#[cfg(target_os = "macos")]
frame_gen: frame_gen::FrameGen::new(),
}
}
Self {
clock,
viewer,
#[cfg(target_os = "macos")]
frame_gen: frame_gen::FrameGen::new(),
}
}
fn tick(&mut self, cx: &mut Context<Self>) {
// Simulate the engine: advance the clock while playing.
self.clock.update(cx, |clock, _cx| {
if clock.playing {
clock.frame = Frame((clock.frame.0 + 1) % (30 * 60 * 5));
}
});
// Feed a fresh frame (macOS only).
#[cfg(target_os = "macos")]
{
let buffer = self.frame_gen.next();
self.viewer
.update(cx, |viewer, cx| viewer.set_frame_source(Some(buffer.into()), cx));
}
cx.notify();
}
fn tick(&mut self, cx: &mut Context<Self>) {
// Simulate the engine: advance the clock while playing.
self.clock.update(cx, |clock, _cx| {
if clock.playing {
clock.frame = Frame((clock.frame.0 + 1) % (30 * 60 * 5));
}
});
// Feed a fresh frame (macOS only).
#[cfg(target_os = "macos")]
{
let buffer = self.frame_gen.next();
self.viewer.update(cx, |viewer, cx| {
viewer.set_frame_source(Some(buffer.into()), cx)
});
}
cx.notify();
}
}
impl Render for Example {
fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
div().size_full().child(self.viewer.clone())
}
fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
div().size_full().child(self.viewer.clone())
}
}
fn main() {
gpui_platform::application().run(|cx: &mut App| {
cx.init_colors();
let bounds = Bounds::centered(None, size(px(960.0), px(540.0)), cx);
cx.open_window(
WindowOptions {
window_bounds: Some(WindowBounds::Windowed(bounds)),
..Default::default()
},
|window, cx| cx.new(|cx| Example::new(window, cx)),
)
.expect("Failed to open window");
gpui_platform::application().run(|cx: &mut App| {
cx.init_colors();
let bounds = Bounds::centered(None, size(px(960.0), px(540.0)), cx);
cx.open_window(
WindowOptions {
window_bounds: Some(WindowBounds::Windowed(bounds)),
..Default::default()
},
|window, cx| cx.new(|cx| Example::new(window, cx)),
)
.expect("Failed to open window");
cx.activate(true);
cx.on_window_closed(|cx, _| {
if cx.windows().is_empty() {
cx.quit();
}
})
.detach();
});
cx.activate(true);
cx.on_window_closed(|cx, _| {
if cx.windows().is_empty() {
cx.quit();
}
})
.detach();
});
}