Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Statically typed selectors, take two. #993

Merged
merged 12 commits into from
May 28, 2020
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ This means that druid no longer requires cairo on macOS and uses Core Graphics i
- Replaced `NEW_WINDOW`, `SET_MENU` and `SHOW_CONTEXT_MENU` commands with methods on `EventCtx` and `DelegateCtx`. ([#931] by [@finnerale])
- Replaced `Command::one_shot` and `::take_object` with a `SingleUse` payload wrapper type. ([#959] by [@finnerale])
- Renamed `WidgetPod` methods: `paint` to `paint_raw`, `paint_with_offset` to `paint`, `paint_with_offset_always` to `paint_always`. ([#980] by [@totsteps])
- `Command` and `Selector` have been reworked and are now statically typed, similarly to `Env` and `Key`. ([#993] by [@finnerale])

### Deprecated

Expand Down Expand Up @@ -242,6 +243,7 @@ This means that druid no longer requires cairo on macOS and uses Core Graphics i
[#984]: https://github.com/xi-editor/druid/pull/984
[#990]: https://github.com/xi-editor/druid/pull/990
[#991]: https://github.com/xi-editor/druid/pull/991
[#993]: https://github.com/xi-editor/druid/pull/993

## [0.5.0] - 2020-04-01

Expand Down
25 changes: 10 additions & 15 deletions druid/examples/blocking_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ use druid::{

use druid::widget::{Button, Either, Flex, Label};

const START_SLOW_FUNCTION: Selector = Selector::new("start_slow_function");
const START_SLOW_FUNCTION: Selector<u32> = Selector::new("start_slow_function");

const FINISH_SLOW_FUNCTION: Selector = Selector::new("finish_slow_function");
const FINISH_SLOW_FUNCTION: Selector<u32> = Selector::new("finish_slow_function");

struct Delegate {
eventsink: ExtEventSink,
Expand Down Expand Up @@ -61,20 +61,15 @@ impl AppDelegate<AppState> for Delegate {
data: &mut AppState,
_env: &Env,
) -> bool {
match cmd.selector {
START_SLOW_FUNCTION => {
data.processing = true;
wrapped_slow_function(self.eventsink.clone(), data.value);
true
}
FINISH_SLOW_FUNCTION => {
data.processing = false;
let number = cmd.get_object::<u32>().expect("api violation");
data.value = *number;
true
}
_ => true,
if cmd.is(START_SLOW_FUNCTION) {
data.processing = true;
wrapped_slow_function(self.eventsink.clone(), data.value);
}
if let Some(number) = cmd.get(FINISH_SLOW_FUNCTION) {
data.processing = false;
data.value = *number;
}
true
}
}

Expand Down
6 changes: 3 additions & 3 deletions druid/examples/ext_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use druid::kurbo::RoundedRect;
use druid::widget::prelude::*;
use druid::{AppLauncher, Color, Data, LocalizedString, Rect, Selector, WidgetExt, WindowDesc};

const SET_COLOR: Selector = Selector::new("event-example.set-color");
const SET_COLOR: Selector<Color> = Selector::new("event-example.set-color");

/// A widget that displays a color.
struct ColorWell;
Expand Down Expand Up @@ -53,8 +53,8 @@ impl ColorWell {
impl Widget<MyColor> for ColorWell {
fn event(&mut self, ctx: &mut EventCtx, event: &Event, data: &mut MyColor, _env: &Env) {
match event {
Event::Command(cmd) if cmd.selector == SET_COLOR => {
data.0 = cmd.get_object::<Color>().unwrap().clone();
Event::Command(cmd) if cmd.is(SET_COLOR) => {
data.0 = cmd.get_unchecked(SET_COLOR).clone();
ctx.request_paint();
}
_ => (),
Expand Down
15 changes: 4 additions & 11 deletions druid/examples/identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ use druid::{

const CYCLE_DURATION: Duration = Duration::from_millis(100);

const FREEZE_COLOR: Selector = Selector::new("identity-example.freeze-color");
const FREEZE_COLOR: Selector<Color> = Selector::new("identity-example.freeze-color");
const UNFREEZE_COLOR: Selector = Selector::new("identity-example.unfreeze-color");

/// Honestly: it's just a color in fancy clothing.
Expand Down Expand Up @@ -109,20 +109,13 @@ impl Widget<OurData> for ColorWell {
self.token = ctx.request_timer(CYCLE_DURATION);
ctx.request_paint();
}

Event::WindowConnected if self.randomize => {
self.token = ctx.request_timer(CYCLE_DURATION);
}

Event::Command(cmd) if cmd.selector == FREEZE_COLOR => {
self.frozen = cmd
.get_object::<Color>()
.ok()
.cloned()
.expect("payload is always a Color")
.into();
Event::Command(cmd) if cmd.is(FREEZE_COLOR) => {
self.frozen = cmd.get(FREEZE_COLOR).cloned();
}
Event::Command(cmd) if cmd.selector == UNFREEZE_COLOR => self.frozen = None,
Event::Command(cmd) if cmd.is(UNFREEZE_COLOR) => self.frozen = None,
_ => (),
}
}
Expand Down
16 changes: 8 additions & 8 deletions druid/examples/multiwin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use druid::{
};
use log::info;

const MENU_COUNT_ACTION: Selector = Selector::new("menu-count-action");
const MENU_COUNT_ACTION: Selector<usize> = Selector::new("menu-count-action");
const MENU_INCREMENT_ACTION: Selector = Selector::new("menu-increment-action");
const MENU_DECREMENT_ACTION: Selector = Selector::new("menu-decrement-action");
const MENU_SWITCH_GLOW_ACTION: Selector = Selector::new("menu-switch-glow");
Expand Down Expand Up @@ -155,16 +155,16 @@ impl AppDelegate<State> for Delegate {
data: &mut State,
_env: &Env,
) -> bool {
match cmd.selector {
sys_cmds::NEW_FILE => {
match cmd {
_ if cmd.is(sys_cmds::NEW_FILE) => {
let new_win = WindowDesc::new(ui_builder)
.menu(make_menu(data))
.window_size((data.selected as f64 * 100.0 + 300.0, 500.0));
ctx.new_window(new_win);
false
}
MENU_COUNT_ACTION => {
data.selected = *cmd.get_object().unwrap();
_ if cmd.is(MENU_COUNT_ACTION) => {
data.selected = *cmd.get_unchecked(MENU_COUNT_ACTION);
let menu = make_menu::<State>(data);
for id in &self.windows {
ctx.set_menu(menu.clone(), *id);
Expand All @@ -173,23 +173,23 @@ impl AppDelegate<State> for Delegate {
}
// wouldn't it be nice if a menu (like a button) could just mutate state
// directly if desired?
MENU_INCREMENT_ACTION => {
_ if cmd.is(MENU_INCREMENT_ACTION) => {
data.menu_count += 1;
let menu = make_menu::<State>(data);
for id in &self.windows {
ctx.set_menu(menu.clone(), *id);
}
false
}
MENU_DECREMENT_ACTION => {
_ if cmd.is(MENU_DECREMENT_ACTION) => {
data.menu_count = data.menu_count.saturating_sub(1);
let menu = make_menu::<State>(data);
for id in &self.windows {
ctx.set_menu(menu.clone(), *id);
}
false
}
MENU_SWITCH_GLOW_ACTION => {
_ if cmd.is(MENU_SWITCH_GLOW_ACTION) => {
data.glow_hot = !data.glow_hot;
false
}
Expand Down
38 changes: 16 additions & 22 deletions druid/examples/open_save.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

use druid::widget::{Align, Button, Flex, TextBox};
use druid::{
AppDelegate, AppLauncher, Command, DelegateCtx, Env, FileDialogOptions, FileInfo, FileSpec,
commands, AppDelegate, AppLauncher, Command, DelegateCtx, Env, FileDialogOptions, FileSpec,
LocalizedString, Target, Widget, WindowDesc,
};

Expand Down Expand Up @@ -77,30 +77,24 @@ impl AppDelegate<String> for Delegate {
data: &mut String,
_env: &Env,
) -> bool {
match cmd.selector {
druid::commands::SAVE_FILE => {
if let Ok(file_info) = cmd.get_object::<FileInfo>() {
if let Err(e) = std::fs::write(file_info.path(), &data[..]) {
println!("Error writing file: {}", e);
}
}
true
if let Some(Some(file_info)) = cmd.get(commands::SAVE_FILE) {
if let Err(e) = std::fs::write(file_info.path(), &data[..]) {
println!("Error writing file: {}", e);
}
druid::commands::OPEN_FILE => {
if let Ok(file_info) = cmd.get_object::<FileInfo>() {
match std::fs::read_to_string(file_info.path()) {
Ok(s) => {
let first_line = s.lines().next().unwrap_or("");
*data = first_line.to_owned();
}
Err(e) => {
println!("Error opening file: {}", e);
}
}
return true;
}
if let Some(file_info) = cmd.get(commands::OPEN_FILE) {
match std::fs::read_to_string(file_info.path()) {
Ok(s) => {
let first_line = s.lines().next().unwrap_or("");
*data = first_line.to_owned();
}
Err(e) => {
println!("Error opening file: {}", e);
}
true
}
_ => false,
return true;
}
false
}
}
4 changes: 2 additions & 2 deletions druid/src/app_delegate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl<'a> DelegateCtx<'a> {
pub fn new_window<T: Any>(&mut self, desc: WindowDesc<T>) {
if self.app_data_type == TypeId::of::<T>() {
self.submit_command(
Command::new(commands::NEW_WINDOW, SingleUse::new(desc)),
Command::new(commands::NEW_WINDOW, SingleUse::new(Box::new(desc))),
Target::Global,
);
} else {
Expand All @@ -77,7 +77,7 @@ impl<'a> DelegateCtx<'a> {
pub fn set_menu<T: Any>(&mut self, menu: MenuDesc<T>, window: WindowId) {
if self.app_data_type == TypeId::of::<T>() {
self.submit_command(
Command::new(commands::SET_MENU, menu),
Command::new(commands::SET_MENU, Box::new(menu)),
Target::Window(window),
);
} else {
Expand Down
Loading