Skip to content
This repository has been archived by the owner on Jun 5, 2024. It is now read-only.

Commit

Permalink
TMP 4
Browse files Browse the repository at this point in the history
  • Loading branch information
PoignardAzur committed Apr 17, 2024
1 parent 520c67c commit c131eb8
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 313 deletions.
24 changes: 6 additions & 18 deletions src/app_delegate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,22 @@ use std::collections::HashMap;
use tracing::trace;

use crate::action::Action;
use crate::command::{Command, CommandQueue};
use crate::ext_event::{ExtEventQueue, ExtEventSink};
use crate::widget::{StoreInWidgetMut, WidgetMut, WidgetRef};
use crate::{
Event, Handled, SingleUse, Target, Widget, WidgetId, WindowDescription, WindowId, WindowRoot,
};
use crate::{Event, Handled, Widget, WidgetId, WindowDescription, WindowId, WindowRoot};

/// A context provided to [`AppDelegate`] methods.
pub struct DelegateCtx<'a, 'b> {
pub struct DelegateCtx<'a> {
//pub(crate) command_queue: &'a mut CommandQueue,
pub(crate) ext_event_queue: &'a ExtEventQueue,
// FIXME - Ideally, we'd like to get a hashmap of all root widgets,
// but that creates "aliasing mutable references" problems
// See issue #17
pub(crate) main_root_widget: WidgetMut<'a, 'b, Box<dyn Widget>>,
pub(crate) main_root_widget: WidgetMut<'a, Box<dyn Widget>>,
//pub(crate) active_windows: &'a mut HashMap<WindowId, WindowRoot>,
}

impl<'a, 'b> DelegateCtx<'a, 'b> {
impl<'a> DelegateCtx<'a> {
#[cfg(FALSE)]
pub fn submit_command(&mut self, command: impl Into<Command>) {
self.command_queue
Expand All @@ -53,7 +50,7 @@ impl<'a, 'b> DelegateCtx<'a, 'b> {
/// Try to return a [`WidgetMut`] to the root widget.
///
/// Returns null if the returned type doesn't match the root widget type.
pub fn try_get_root<W: Widget + StoreInWidgetMut>(&mut self) -> Option<WidgetMut<'_, 'b, W>> {
pub fn try_get_root<W: Widget + StoreInWidgetMut>(&mut self) -> Option<WidgetMut<'_, W>> {
self.main_root_widget.downcast()
}

Expand All @@ -62,7 +59,7 @@ impl<'a, 'b> DelegateCtx<'a, 'b> {
/// ## Panics
///
/// Panics if the returned type doesn't match the root widget type.
pub fn get_root<W: Widget + StoreInWidgetMut>(&mut self) -> WidgetMut<'_, 'b, W> {
pub fn get_root<W: Widget + StoreInWidgetMut>(&mut self) -> WidgetMut<'_, W> {
self.main_root_widget.downcast().expect("wrong widget type")
}
}
Expand All @@ -81,15 +78,6 @@ pub trait AppDelegate {
Handled::No
}

/// The handler for [`Command`]s.
///
/// This function receives all command events, before they are passed down
/// the tree. If it returns [`Handled::Yes`], commands are short-circuited.
fn on_command(&mut self, ctx: &mut DelegateCtx, cmd: &Command) -> Handled {
#![allow(unused)]
Handled::No
}

/// The handler for [`Action`]s.
///
/// Note: Actions are still a WIP part of masonry.
Expand Down
39 changes: 5 additions & 34 deletions src/ext_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@
// details.

//! Simple handle for submitting external events.
#![allow(unused)]

use std::any::Any;
use std::collections::VecDeque;
use std::sync::{Arc, Mutex};

use druid_shell::IdleHandle;

use crate::command::SelectorSymbol;
use crate::platform::EXT_EVENT_IDLE_TOKEN;
use crate::promise::PromiseResult;
use crate::widget::WidgetId;
use crate::{Selector, Target, WindowId};

// FIXME - Remove
pub struct WindowId;

pub(crate) enum ExtMessage {
Command(SelectorSymbol, Box<dyn Any + Send>, Target),
Promise(PromiseResult, WidgetId, WindowId),
}

Expand Down Expand Up @@ -78,35 +78,6 @@ impl ExtEventQueue {
}

impl ExtEventSink {
/// Submit a [`Command`] to the running application.
///
/// [`Command`] is not thread safe, so you cannot submit it directly;
/// instead you have to pass the [`Selector`] and the payload
/// separately, and it will be turned into a [`Command`] when it is received.
///
/// For the **target** argument, [`Target::Auto`] is equivalent to [`Target::Global`].
///
/// [`Command`]: struct.Command.html
/// [`Target::Auto`]: enum.Target.html#variant.Auto
/// [`Target::Global`]: enum.Target.html#variant.Global
pub fn submit_command<T: Any + Send>(
&self,
selector: Selector<T>,
payload: impl Into<Box<T>>,
target: impl Into<Target>,
) -> Result<(), ExtEventError> {
let target = target.into();
let payload = payload.into();
if let Some(handle) = self.handle.lock().unwrap().as_mut() {
handle.schedule_idle(EXT_EVENT_IDLE_TOKEN);
}
self.queue
.lock()
.map_err(|_| ExtEventError)?
.push_back(ExtMessage::Command(selector.symbol(), payload, target));
Ok(())
}

#[allow(missing_docs)]
pub fn resolve_promise(
&self,
Expand All @@ -115,7 +86,7 @@ impl ExtEventSink {
target_window: WindowId,
) -> Result<(), ExtEventError> {
if let Some(handle) = self.handle.lock().unwrap().as_mut() {
handle.schedule_idle(EXT_EVENT_IDLE_TOKEN);
todo!()
}
self.queue
.lock()
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ pub mod debug_values;
pub use action::Action;
pub use box_constraints::BoxConstraints;
pub use contexts::{EventCtx, LayoutCtx, LifeCycleCtx, PaintCtx, WidgetCtx};
pub use event::{InternalLifeCycle, LifeCycle, StatusChange, WindowTheme};
pub use event::{InternalLifeCycle, LifeCycle, PointerEvent, StatusChange, TextEvent, WindowTheme};
pub use kurbo::{Affine, Insets, Point, Rect, Size, Vec2};
pub use util::{AsAny, Handled};
pub use vello::peniko::{Color, Gradient};
Expand Down
9 changes: 0 additions & 9 deletions src/platform/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
// This software is licensed under Apache License 2.0 and distributed on an
// "as-is" basis without warranties of any kind. See the LICENSE file for
// details.

#[cfg(not(tarpaulin_include))]
mod win_handler;
#[cfg(not(tarpaulin_include))]
mod window_description;

pub use win_handler::{DialogInfo, MasonryAppHandler, MasonryWinHandler};
pub(crate) use win_handler::{EXT_EVENT_IDLE_TOKEN, RUN_COMMANDS_TOKEN};
pub use window_description::{WindowConfig, WindowDescription, WindowId, WindowSizePolicy};
14 changes: 2 additions & 12 deletions src/platform/window_description.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub struct WindowId(u64);
/// .window_size((400.0, 400.0));
/// ```
pub struct WindowDescription {
#[allow(dead_code)]
pub(crate) root: Box<dyn Widget>,
pub(crate) title: ArcStr,
pub(crate) config: WindowConfig,
Expand All @@ -33,18 +34,7 @@ pub struct WindowDescription {
pub id: WindowId,
}

/// Defines how a windows size should be determined
#[derive(Copy, Clone, Debug, Default, PartialEq)]
pub enum WindowSizePolicy {
/// Use the content of the window to determine the size.
///
/// If you use this option, your root widget will be passed infinite constraints;
/// you are responsible for ensuring that your content picks an appropriate size.
Content,
/// Use the provided window size.
#[default]
User,
}
pub use crate::render_root::WindowSizePolicy;

/// Window configuration that can be applied to a [WindowBuilder], or to an existing [WindowHandle].
///
Expand Down
7 changes: 4 additions & 3 deletions src/promise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use std::any::Any;
use std::num::NonZeroU64;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, Mutex};

#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
Expand All @@ -24,9 +25,9 @@ pub struct PromiseResult {

impl PromiseTokenId {
pub fn next() -> PromiseTokenId {
use druid_shell::Counter;
static WIDGET_ID_COUNTER: Counter = Counter::new();
PromiseTokenId(WIDGET_ID_COUNTER.next_nonzero())
static WIDGET_ID_COUNTER: AtomicU64 = AtomicU64::new(1);
let id = WIDGET_ID_COUNTER.fetch_add(1, Ordering::Relaxed);
PromiseTokenId(id.try_into().unwrap())
}

pub fn to_raw(self) -> u64 {
Expand Down
2 changes: 0 additions & 2 deletions src/widget/tests/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

//! Tests related to layout.
#![allow(unused_imports)]

use crate::kurbo::{Insets, Size};
use crate::testing::{widget_ids, ModularWidget, TestHarness, TestWidgetExt};
use crate::widget::{Flex, SizedBox};
Expand Down
Loading

0 comments on commit c131eb8

Please sign in to comment.