Skip to content

Commit

Permalink
Merge pull request #424 from kas-gui/work3
Browse files Browse the repository at this point in the history
kas::message→messages, class→classes
  • Loading branch information
dhardy authored Dec 11, 2023
2 parents a979f04 + d8a447b commit 11997d6
Show file tree
Hide file tree
Showing 44 changed files with 157 additions and 149 deletions.
6 changes: 2 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,7 @@ jobs:
# -A clippy::collapsible-if \
# -A clippy::collapsible_else_if \
# -A clippy::module-inception \
# -A clippy::too-many-arguments \
# -A clippy::comparison_chain \
# -A clippy::or-fun-call \
# -A clippy::redundant_pattern_matching \
# -A clippy::type_complexity \
# -A clippy::if_same_then_else \
# -A clippy::single-match \
# -A clippy::unit_arg
2 changes: 1 addition & 1 deletion clippy.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
single-char-binding-names-threshold = 7
type-complexity-threshold = 400
type-complexity-threshold = 600
7 changes: 3 additions & 4 deletions crates/kas-core/src/app/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,17 @@

//! [`Application`] and supporting elements
use super::{AppGraphicsBuilder, AppState, Platform, ProxyAction, Result};
use super::{AppData, AppGraphicsBuilder, AppState, Platform, ProxyAction, Result};
use crate::config::Options;
use crate::draw::{DrawShared, DrawSharedImpl};
use crate::event;
use crate::theme::{self, Theme, ThemeConfig};
use crate::util::warn_about_error;
use crate::{impl_scope, AppData, Window, WindowId};
use crate::{impl_scope, Window, WindowId};
use std::cell::RefCell;
use std::rc::Rc;
use winit::event_loop::{EventLoop, EventLoopBuilder, EventLoopProxy};

/// Application pre-launch state
pub struct Application<Data: AppData, G: AppGraphicsBuilder, T: Theme<G::Shared>> {
el: EventLoop<ProxyAction>,
windows: Vec<Box<super::Window<Data, G::Surface, T>>>,
Expand Down Expand Up @@ -335,7 +334,7 @@ impl Proxy {
msg: M,
) -> std::result::Result<(), ClosedError> {
self.0
.send_event(ProxyAction::Message(kas::erased::SendErased::new(msg)))
.send_event(ProxyAction::Message(kas::messages::SendErased::new(msg)))
.map_err(|_| ClosedError)
}

Expand Down
8 changes: 4 additions & 4 deletions crates/kas-core/src/app/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

//! Event loop and handling
use super::{AppState, Pending};
use super::{AppData, AppState, Pending};
use super::{ProxyAction, Window, WindowSurface};
use kas::theme::Theme;
use kas::{Action, AppData, WindowId};
use kas::{Action, WindowId};
use std::collections::HashMap;
use std::time::Instant;
use winit::event::{Event, StartCause};
Expand Down Expand Up @@ -118,7 +118,7 @@ where
}
}
ProxyAction::Message(msg) => {
let mut stack = crate::ErasedStack::new();
let mut stack = crate::messages::MessageStack::new();
stack.push_erased(msg.into_erased());
self.state.handle_messages(&mut stack);
}
Expand Down Expand Up @@ -215,7 +215,7 @@ where
elwt.set_control_flow(ControlFlow::Poll);
} else {
for (_, window) in self.windows.iter_mut() {
window.handle_action(&mut self.state, action);
window.handle_action(&self.state, action);
}
}
}
Expand Down
31 changes: 29 additions & 2 deletions crates/kas-core/src/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ mod common;
#[cfg(winit)] mod window;

#[cfg(winit)] use crate::WindowId;
use crate::{messages::MessageStack, Action};
#[cfg(winit)] use app::PlatformWrapper;
#[cfg(winit)] use event_loop::Loop as EventLoop;
#[cfg(winit)] pub(crate) use shared::{AppShared, AppState};
Expand All @@ -27,9 +28,35 @@ pub use common::{Error, Platform, Result};
#[cfg_attr(not(feature = "internal_doc"), doc(hidden))]
pub extern crate raw_window_handle;

/// Application state
///
/// Kas allows application state to be stored both in the widget tree (in
/// `Adapt` nodes and user-defined widgets) and by the application root (shared
/// across windows). This trait must be implemented by the latter.
///
/// When no top-level data is required, use `()` which implements this trait.
pub trait AppData: 'static {
/// Handle messages
///
/// This is the last message handler: it is called when, after traversing
/// the widget tree (see [kas::event] module doc), a message is left on the
/// stack. Unhandled messages will result in warnings in the log.
///
/// The method returns an [`Action`], usually either [`Action::empty`]
/// (nothing to do) or [`Action::UPDATE`] (to update widgets).
/// This action affects all windows.
fn handle_messages(&mut self, messages: &mut MessageStack) -> Action;
}

impl AppData for () {
fn handle_messages(&mut self, _: &mut MessageStack) -> Action {
Action::empty()
}
}

#[crate::autoimpl(Debug)]
#[cfg(winit)]
enum Pending<A: kas::AppData, S: WindowSurface, T: kas::theme::Theme<S::Shared>> {
enum Pending<A: AppData, S: WindowSurface, T: kas::theme::Theme<S::Shared>> {
AddPopup(WindowId, WindowId, kas::PopupDescriptor),
// NOTE: we don't need S, T here if we construct the Window later.
// But this way we can pass a single boxed value.
Expand All @@ -43,7 +70,7 @@ enum Pending<A: kas::AppData, S: WindowSurface, T: kas::theme::Theme<S::Shared>>
enum ProxyAction {
CloseAll,
Close(WindowId),
Message(kas::erased::SendErased),
Message(kas::messages::SendErased),
WakeAsync,
}

Expand Down
6 changes: 3 additions & 3 deletions crates/kas-core/src/app/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@

//! Shared state
use super::{Error, Pending, Platform, WindowSurface};
use super::{AppData, Error, Pending, Platform, WindowSurface};
use kas::config::Options;
use kas::draw::DrawShared;
use kas::theme::{Theme, ThemeControl};
use kas::util::warn_about_error;
use kas::{draw, Action, AppData, ErasedStack, WindowId};
use kas::{draw, messages::MessageStack, Action, WindowId};
use std::any::TypeId;
use std::cell::RefCell;
use std::collections::VecDeque;
Expand Down Expand Up @@ -85,7 +85,7 @@ where
}

#[inline]
pub(crate) fn handle_messages(&mut self, messages: &mut ErasedStack) {
pub(crate) fn handle_messages(&mut self, messages: &mut MessageStack) {
if messages.reset_and_has_any() {
let action = self.data.handle_messages(messages);
self.shared.pending.push_back(Pending::Action(action));
Expand Down
14 changes: 7 additions & 7 deletions crates/kas-core/src/app/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
use super::common::WindowSurface;
use super::shared::{AppSharedState, AppState};
use super::ProxyAction;
use super::{AppData, ProxyAction};
use kas::cast::{Cast, Conv};
use kas::draw::{color::Rgba, AnimationState, DrawSharedImpl};
use kas::event::{config::WindowConfig, ConfigCx, CursorIcon, EventState};
use kas::geom::{Coord, Rect, Size};
use kas::layout::SolveCache;
use kas::theme::{DrawCx, SizeCx, ThemeSize};
use kas::theme::{Theme, Window as _};
use kas::{autoimpl, Action, AppData, ErasedStack, Id, Layout, LayoutExt, Widget, WindowId};
use kas::{autoimpl, messages::MessageStack, Action, Id, Layout, LayoutExt, Widget, WindowId};
use std::mem::take;
use std::time::{Duration, Instant};
use winit::event::WindowEvent;
Expand Down Expand Up @@ -241,7 +241,7 @@ impl<A: AppData, S: WindowSurface, T: Theme<S::Shared>> Window<A, S, T> {
}
WindowEvent::RedrawRequested => self.do_draw(state).is_err(),
event => {
let mut messages = ErasedStack::new();
let mut messages = MessageStack::new();
self.ev_state
.with(&mut state.shared, window, &mut messages, |cx| {
cx.handle_winit(&mut self.widget, &state.data, event);
Expand All @@ -266,7 +266,7 @@ impl<A: AppData, S: WindowSurface, T: Theme<S::Shared>> Window<A, S, T> {
let Some(ref window) = self.window else {
return (Action::empty(), None);
};
let mut messages = ErasedStack::new();
let mut messages = MessageStack::new();
let action = self.ev_state.flush_pending(
&mut state.shared,
window,
Expand Down Expand Up @@ -341,7 +341,7 @@ impl<A: AppData, S: WindowSurface, T: Theme<S::Shared>> Window<A, S, T> {
return None;
};
let widget = self.widget.as_node(&state.data);
let mut messages = ErasedStack::new();
let mut messages = MessageStack::new();
self.ev_state
.with(&mut state.shared, window, &mut messages, |cx| {
cx.update_timer(widget)
Expand All @@ -359,7 +359,7 @@ impl<A: AppData, S: WindowSurface, T: Theme<S::Shared>> Window<A, S, T> {
let Some(ref window) = self.window else {
return;
};
let mut messages = ErasedStack::new();
let mut messages = MessageStack::new();
self.ev_state
.with(&mut state.shared, window, &mut messages, |cx| {
self.widget.add_popup(cx, &state.data, id, popup)
Expand All @@ -376,7 +376,7 @@ impl<A: AppData, S: WindowSurface, T: Theme<S::Shared>> Window<A, S, T> {
self.ev_state.action(Id::ROOT, Action::CLOSE);
} else if let Some(window) = self.window.as_ref() {
let widget = &mut self.widget;
let mut messages = ErasedStack::new();
let mut messages = MessageStack::new();
self.ev_state
.with(&mut state.shared, window, &mut messages, |cx| {
widget.remove_popup(cx, id)
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion crates/kas-core/src/core/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use crate::event::{ConfigCx, Event, EventCx, FocusSource, IsUsed, Scroll, Unused, Used};
#[cfg(debug_assertions)] use crate::util::IdentifyWidget;
use crate::{Erased, Events, Id, Layout, NavAdvance, Node, Widget};
use crate::{messages::Erased, Events, Id, Layout, NavAdvance, Node, Widget};

/// Generic implementation of [`Widget::_send`]
pub fn _send<W: Events>(
Expand Down
2 changes: 1 addition & 1 deletion crates/kas-core/src/core/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::event::{ConfigCx, Event, EventCx, IsUsed};
use crate::geom::{Coord, Rect};
use crate::layout::{AxisInfo, SizeRules};
use crate::theme::{DrawCx, SizeCx};
use crate::{Erased, Id, Layout, NavAdvance};
use crate::{messages::Erased, Id, Layout, NavAdvance};

#[cfg(not(feature = "unsafe_node"))]
trait NodeT {
Expand Down
2 changes: 1 addition & 1 deletion crates/kas-core/src/core/widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use super::{Layout, Node};
#[allow(unused)] use crate::event::Used;
use crate::event::{ConfigCx, Event, EventCx, IsUsed, Scroll, Unused};
use crate::{Erased, Id};
use crate::{messages::Erased, Id};
use kas_macros::autoimpl;

#[allow(unused)] use kas_macros as macros;
Expand Down
2 changes: 1 addition & 1 deletion crates/kas-core/src/event/cx/cx_pub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::geom::{Offset, Vec2};
use crate::theme::{SizeCx, ThemeControl};
#[cfg(all(wayland_platform, feature = "clipboard"))]
use crate::util::warn_about_error;
use crate::{Action, Erased, HasId, Id, Window, WindowId};
use crate::{messages::Erased, Action, HasId, Id, Window, WindowId};
#[allow(unused)] use crate::{Events, Layout}; // for doc-links

/// Public API
Expand Down
5 changes: 3 additions & 2 deletions crates/kas-core/src/event/cx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ use super::*;
use crate::app::{AppShared, Platform, WindowDataErased};
use crate::cast::Cast;
use crate::geom::Coord;
use crate::messages::{Erased, MessageStack};
use crate::util::WidgetHierarchy;
use crate::LayoutExt;
use crate::{Action, Erased, ErasedStack, Id, NavAdvance, Node, Widget, WindowId};
use crate::{Action, Id, NavAdvance, Node, Widget, WindowId};

mod config;
mod cx_pub;
Expand Down Expand Up @@ -367,7 +368,7 @@ pub struct EventCx<'a> {
state: &'a mut EventState,
shared: &'a mut dyn AppShared,
window: &'a dyn WindowDataErased,
messages: &'a mut ErasedStack,
messages: &'a mut MessageStack,
last_child: Option<usize>,
scroll: Scroll,
}
Expand Down
4 changes: 2 additions & 2 deletions crates/kas-core/src/event/cx/platform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ impl EventState {
&'a mut self,
shared: &'a mut dyn AppShared,
window: &'a dyn WindowDataErased,
messages: &'a mut ErasedStack,
messages: &'a mut MessageStack,
f: F,
) {
let mut cx = EventCx {
Expand All @@ -129,7 +129,7 @@ impl EventState {
&'a mut self,
shared: &'a mut dyn AppShared,
window: &'a dyn WindowDataErased,
messages: &'a mut ErasedStack,
messages: &'a mut MessageStack,
win: &mut Window<A>,
data: &A,
) -> Action {
Expand Down
2 changes: 1 addition & 1 deletion crates/kas-core/src/event/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
//! - If the message stack is non-empty (see [`EventCx::push`]),
//! call [`Events::handle_messages`].
//! 7. If the message stack is not empty, call
//! [`AppData::handle_messages`](crate::AppData::handle_messages).
//! [`AppData::handle_messages`](crate::app::AppData::handle_messages).
//! 8. Clear any messages still on the message stack, printing a warning to the
//! log. Messages *should* be handled during unwinding, though not doing so
//! is safe (and possibly useful during development).
Expand Down
4 changes: 2 additions & 2 deletions crates/kas-core/src/hidden.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
//! hidden by default and direct usage (outside of kas crates) is
//! not supported (i.e. **changes are not considered breaking**).
use crate::class::HasStr;
use crate::classes::HasStr;
use crate::event::{ConfigCx, Event, EventCx, IsUsed};
use crate::geom::{Coord, Offset, Rect};
use crate::layout::{Align, AxisInfo, SizeRules};
use crate::text::{Text, TextApi};
use crate::theme::{DrawCx, SizeCx, TextClass};
use crate::{Erased, Id, Layout, NavAdvance, Node, Widget};
use crate::{messages::Erased, Id, Layout, NavAdvance, Node, Widget};
use kas_macros::{autoimpl, impl_scope};

impl_scope! {
Expand Down
6 changes: 2 additions & 4 deletions crates/kas-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,12 @@ extern crate self as kas;
mod action;
mod core;
mod decorations;
mod erased;
mod popup;
mod root;

pub use crate::core::*;
pub use action::Action;
pub use decorations::Decorations;
pub use erased::{AppData, Erased, ErasedStack};
pub use kas_macros::*;
#[doc(inline)] pub use popup::Popup;
#[doc(inline)] pub(crate) use popup::PopupDescriptor;
Expand All @@ -40,7 +38,7 @@ pub use root::{Window, WindowCommand, WindowId};

// public implementations:
pub mod app;
pub mod class;
pub mod classes;
pub mod config;
pub mod dir;
pub mod draw;
Expand All @@ -50,7 +48,7 @@ pub mod geom;
#[cfg_attr(doc_cfg, doc(cfg(internal_doc)))]
pub mod hidden;
pub mod layout;
pub mod message;
pub mod messages;
pub mod prelude;
pub mod text;
pub mod theme;
Expand Down
25 changes: 0 additions & 25 deletions crates/kas-core/src/message.rs

This file was deleted.

Loading

0 comments on commit 11997d6

Please sign in to comment.