Skip to content

Commit

Permalink
Simplify app data type checks. (#945)
Browse files Browse the repository at this point in the history
  • Loading branch information
xStrom authored May 16, 2020
1 parent 0acb07b commit a480919
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 42 deletions.
15 changes: 6 additions & 9 deletions druid/src/app_delegate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,14 @@ use std::{
collections::VecDeque,
};

use crate::{
commands, contexts::StateTypes, Command, Data, Env, Event, MenuDesc, Target, WindowDesc,
WindowId,
};
use crate::{commands, Command, Data, Env, Event, MenuDesc, Target, WindowDesc, WindowId};

/// A context passed in to [`AppDelegate`] functions.
///
/// [`AppDelegate`]: trait.AppDelegate.html
pub struct DelegateCtx<'a> {
pub(crate) command_queue: &'a mut VecDeque<(Target, Command)>,
pub(crate) state_types: StateTypes,
pub(crate) app_data_type: TypeId,
}

impl<'a> DelegateCtx<'a> {
Expand All @@ -56,13 +53,13 @@ impl<'a> DelegateCtx<'a> {
///
/// [`AppLauncher::launch`]: struct.AppLauncher.html#method.launch
pub fn new_window<T: Any>(&mut self, desc: WindowDesc<T>) {
if self.state_types.window_desc == TypeId::of::<WindowDesc<T>>() {
if self.app_data_type == TypeId::of::<T>() {
self.submit_command(
Command::one_shot(commands::NEW_WINDOW, desc),
Target::Global,
);
} else {
const MSG: &str = "WindowDesc<T> - T must match the application state.";
const MSG: &str = "WindowDesc<T> - T must match the application data type.";
if cfg!(debug_assertions) {
panic!(MSG);
} else {
Expand All @@ -76,13 +73,13 @@ impl<'a> DelegateCtx<'a> {
///
/// [`AppLauncher::launch`]: struct.AppLauncher.html#method.launch
pub fn set_menu<T: Any>(&mut self, menu: MenuDesc<T>, window: WindowId) {
if self.state_types.menu_desc == TypeId::of::<MenuDesc<T>>() {
if self.app_data_type == TypeId::of::<T>() {
self.submit_command(
Command::new(commands::SET_MENU, menu),
Target::Window(window),
);
} else {
const MSG: &str = "MenuDesc<T> - T must match the application state.";
const MSG: &str = "MenuDesc<T> - T must match the application data type.";
if cfg!(debug_assertions) {
panic!(MSG);
} else {
Expand Down
32 changes: 7 additions & 25 deletions druid/src/contexts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,6 @@ use crate::{
Text, TimerToken, Vec2, WidgetId, WindowDesc, WindowHandle, WindowId,
};

/// These allow type checking new windows and menus at runtime
#[derive(Clone, Copy)]
pub(crate) struct StateTypes {
pub window_desc: TypeId,
pub menu_desc: TypeId,
pub context_menu: TypeId,
}

/// A mutable context provided to event handling methods of widgets.
///
/// Widgets should call [`request_paint`] whenever an event causes a change
Expand All @@ -54,7 +46,7 @@ pub struct EventCtx<'a> {
pub(crate) focus_widget: Option<WidgetId>,
pub(crate) is_handled: bool,
pub(crate) is_root: bool,
pub(crate) state_types: StateTypes,
pub(crate) app_data_type: TypeId,
}

/// A mutable context provided to the [`lifecycle`] method on widgets.
Expand Down Expand Up @@ -139,16 +131,6 @@ pub struct PaintCtx<'a, 'b: 'a> {
#[derive(Debug, Clone)]
pub struct Region(Rect);

impl StateTypes {
pub fn new<T: Any>() -> Self {
StateTypes {
window_desc: TypeId::of::<WindowDesc<T>>(),
menu_desc: TypeId::of::<MenuDesc<T>>(),
context_menu: TypeId::of::<ContextMenu<T>>(),
}
}
}

impl<'a> EventCtx<'a> {
#[deprecated(since = "0.5.0", note = "use request_paint instead")]
pub fn invalidate(&mut self) {
Expand Down Expand Up @@ -269,13 +251,13 @@ impl<'a> EventCtx<'a> {
///
/// [`AppLauncher::launch`]: struct.AppLauncher.html#method.launch
pub fn new_window<T: Any>(&mut self, desc: WindowDesc<T>) {
if self.state_types.window_desc == TypeId::of::<WindowDesc<T>>() {
if self.app_data_type == TypeId::of::<T>() {
self.submit_command(
Command::one_shot(commands::NEW_WINDOW, desc),
Target::Global,
);
} else {
const MSG: &str = "WindowDesc<T> - T must match the application state.";
const MSG: &str = "WindowDesc<T> - T must match the application data type.";
if cfg!(debug_assertions) {
panic!(MSG);
} else {
Expand All @@ -289,13 +271,13 @@ impl<'a> EventCtx<'a> {
///
/// [`AppLauncher::launch`]: struct.AppLauncher.html#method.launch
pub fn set_menu<T: Any>(&mut self, menu: MenuDesc<T>) {
if self.state_types.menu_desc == TypeId::of::<MenuDesc<T>>() {
if self.app_data_type == TypeId::of::<T>() {
self.submit_command(
Command::new(commands::SET_MENU, menu),
Target::Window(self.window_id),
);
} else {
const MSG: &str = "MenuDesc<T> - T must match the application state.";
const MSG: &str = "MenuDesc<T> - T must match the application data type.";
if cfg!(debug_assertions) {
panic!(MSG);
} else {
Expand All @@ -309,13 +291,13 @@ impl<'a> EventCtx<'a> {
///
/// [`AppLauncher::launch`]: struct.AppLauncher.html#method.launch
pub fn show_context_menu<T: Any>(&mut self, menu: ContextMenu<T>) {
if self.state_types.context_menu == TypeId::of::<ContextMenu<T>>() {
if self.app_data_type == TypeId::of::<T>() {
self.submit_command(
Command::new(commands::SHOW_CONTEXT_MENU, menu),
Target::Window(self.window_id),
);
} else {
const MSG: &str = "ContextMenu<T> - T must match the application state.";
const MSG: &str = "ContextMenu<T> - T must match the application data type.";
if cfg!(debug_assertions) {
panic!(MSG);
} else {
Expand Down
2 changes: 1 addition & 1 deletion druid/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ impl<T: Data, W: Widget<T>> WidgetPod<T, W> {
is_handled: false,
is_root: false,
focus_widget: ctx.focus_widget,
state_types: ctx.state_types,
app_data_type: ctx.app_data_type,
};

let rect = child_ctx.base_state.layout_rect.unwrap_or_default();
Expand Down
6 changes: 3 additions & 3 deletions druid/src/win_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

//! The implementation of the WinHandler trait (druid-shell integration).
use std::any::Any;
use std::any::{Any, TypeId};
use std::cell::RefCell;
use std::collections::{HashMap, VecDeque};
use std::rc::Rc;
Expand All @@ -35,7 +35,7 @@ use crate::{
WindowId,
};

use crate::{command::sys as sys_cmd, contexts::StateTypes};
use crate::command::sys as sys_cmd;

pub(crate) const RUN_COMMANDS_TOKEN: IdleToken = IdleToken::new(1);

Expand Down Expand Up @@ -189,7 +189,7 @@ impl<T: Data> Inner<T> {
} = self;
let mut ctx = DelegateCtx {
command_queue,
state_types: StateTypes::new::<T>(),
app_data_type: TypeId::of::<T>(),
};
if let Some(delegate) = delegate {
Some(f(delegate, data, env, &mut ctx))
Expand Down
9 changes: 5 additions & 4 deletions druid/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

//! Management of multiple windows.
use std::any::TypeId;
use std::collections::HashMap;
use std::mem;

Expand All @@ -28,9 +29,9 @@ use crate::core::{BaseState, CommandQueue, FocusChange};
use crate::widget::LabelText;
use crate::win_handler::RUN_COMMANDS_TOKEN;
use crate::{
contexts::StateTypes, BoxConstraints, Command, Data, Env, Event, EventCtx, InternalEvent,
InternalLifeCycle, LayoutCtx, LifeCycle, LifeCycleCtx, MenuDesc, PaintCtx, TimerToken,
UpdateCtx, Widget, WidgetId, WidgetPod, WindowDesc,
BoxConstraints, Command, Data, Env, Event, EventCtx, InternalEvent, InternalLifeCycle,
LayoutCtx, LifeCycle, LifeCycleCtx, MenuDesc, PaintCtx, TimerToken, UpdateCtx, Widget,
WidgetId, WidgetPod, WindowDesc,
};

/// A unique identifier for a window.
Expand Down Expand Up @@ -206,7 +207,7 @@ impl<T: Data> Window<T> {
window: &self.handle,
window_id: self.id,
focus_widget: self.focus,
state_types: StateTypes::new::<T>(),
app_data_type: TypeId::of::<T>(),
};

self.root.event(&mut ctx, &event, data, env);
Expand Down

0 comments on commit a480919

Please sign in to comment.