Skip to content

Commit

Permalink
feat(core): 🎸 support WindowFlags to control the window behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
M-Adoo committed Sep 8, 2024
1 parent 7716552 commit 0234054
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Please only add new entries below the [Unreleased](#unreleased---releasedate) he

- **core**: The built-in widget `Class` has been added to enable the sharing of consistent styles across multiple elements and to allow widgets to have different actions and styles in different themes. (#pr, @M-Adoo)
- **core**: The widget `ConstrainedBox` has been added as a built-in widget; now `clamp` can be used as a built-in field. (#pr @M-Adoo)
- **core**: Added `WindowFlags` to regulate the window behavior, with the option of utilizing `WindowFlags::ANIMATIONS` to toggle animations on or off. (#pr @M-Adoo)
- **theme/material**: Define the constant variables of motion. (#pr, @M-Adoo)
- **dev_helper**: Refine the widget test macros. (#pr, @M-Adoo)

Expand Down
14 changes: 12 additions & 2 deletions core/src/animation/animate.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use crate::{prelude::*, ticker::FrameMsg, window::WindowId};
use crate::{
prelude::*,
ticker::FrameMsg,
window::{WindowFlags, WindowId},
};
#[simple_declare]
pub struct Animate<S>
where
Expand Down Expand Up @@ -35,14 +39,20 @@ where
let mut animate_ref = self.write();
let this = &mut *animate_ref;
let wnd_id = this.window_id;
let Some(wnd) = AppCtx::get_window(this.window_id) else { return };

if !wnd.flags().contains(WindowFlags::ANIMATIONS) {
return;
}

let new_to = this.state.get();

if let Some(AnimateInfo { from, to, last_progress, .. }) = &mut this.running_info {
*from = this
.state
.calc_lerp_value(from, to, last_progress.value());
*to = new_to;
} else if let Some(wnd) = AppCtx::get_window(wnd_id) {
} else {
drop(animate_ref);

let animate = self.clone_writer();
Expand Down
7 changes: 6 additions & 1 deletion core/src/test_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser);
pub use crate::timer::Timer;
use crate::{
prelude::*,
window::{ShellWindow, WindowId},
window::{ShellWindow, WindowFlags, WindowId},
};

pub struct Frame {
Expand All @@ -28,6 +28,8 @@ pub fn split_value<T: 'static>(v: T) -> (Watcher<Reader<T>>, Stateful<T>) {
(src.clone_watcher(), src.clone_writer())
}

/// The Window assists in writing unit tests; animations are disabled by
/// default.
#[derive(Clone)]
pub struct TestWindow(pub Rc<Window>);

Expand Down Expand Up @@ -56,6 +58,9 @@ impl TestWindow {
fn new_wnd(root: impl Into<GenWidget>, size: Option<Size>) -> Self {
let _ = NEW_TIMER_FN.set(Timer::new_timer_future);
let wnd = AppCtx::new_window(Box::new(TestShellWindow::new(size)), root.into());
let mut flags = wnd.flags();
flags.remove(WindowFlags::ANIMATIONS);
wnd.set_flags(flags);
wnd.run_frame_tasks();
Self(wnd)
}
Expand Down
19 changes: 19 additions & 0 deletions core/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,20 @@ pub struct Window {
///
/// This widgets it's detached from its parent, but still need to paint.
delay_drop_widgets: RefCell<Vec<(Option<WidgetId>, WidgetId)>>,

flags: Cell<WindowFlags>,
}

bitflags! {
#[derive(Clone, Copy)]
#[doc="A set of flags to control the window behavior."]
pub struct WindowFlags: u32 {
#[doc="If this window enables animation, set this flag to true to \
activate all animations; if this flag is not marked, all animations\
will not run."]
const ANIMATIONS = 1 << 0;
const DEFAULT = Self::ANIMATIONS.bits();
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Hash)]
Expand Down Expand Up @@ -277,6 +291,7 @@ impl Window {
priority_task_queue: PriorityTaskQueue::default(),
shell_wnd: RefCell::new(shell_wnd),
delay_drop_widgets: <_>::default(),
flags: Cell::new(WindowFlags::DEFAULT),
};
let window = Rc::new(window);
window.dispatcher.borrow_mut().init(&window);
Expand All @@ -292,6 +307,10 @@ impl Window {

pub fn shell_wnd(&self) -> &RefCell<Box<dyn ShellWindow>> { &self.shell_wnd }

pub fn flags(&self) -> WindowFlags { self.flags.get() }

pub fn set_flags(&self, flags: WindowFlags) { self.flags.set(flags) }

pub(crate) fn add_focus_node(&self, wid: WidgetId, auto_focus: bool, focus_type: FocusType) {
self
.focus_mgr
Expand Down

0 comments on commit 0234054

Please sign in to comment.