Skip to content

Commit

Permalink
Merge pull request #396 from kas-gui/work1
Browse files Browse the repository at this point in the history
Add Widget::update; use input data
  • Loading branch information
dhardy committed Jul 27, 2023
2 parents c9bdd35 + 6bf48b1 commit d73d0b3
Show file tree
Hide file tree
Showing 92 changed files with 4,406 additions and 4,603 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -111,5 +111,7 @@ jobs:
-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::unit_arg
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ wayland = ["kas-core/wayland"]
# Support X11
x11 = ["kas-core/x11"]

# Optimise Node, NodeMut using unsafe code
unsafe_node = ["kas-core/unsafe_node"]

[dependencies]
kas-core = { version = "0.13.0", path = "crates/kas-core" }
kas-dylib = { version = "0.13.0", path = "crates/kas-dylib", optional = true }
Expand Down
3 changes: 3 additions & 0 deletions crates/kas-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ dark-light = ["dep:dark-light"]
# Support spawning async tasks
spawn = ["dep:async-global-executor"]

# Optimise Node, NodeMut using unsafe code
unsafe_node = []

[build-dependencies]
cfg_aliases = "0.1.1"

Expand Down
14 changes: 10 additions & 4 deletions crates/kas-core/src/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ bitflags! {
#[must_use]
#[derive(Copy, Clone, Default)]
pub struct Action: u32 {
/// No flags
///
/// This is a [zero flag](https://docs.rs/bitflags/latest/bitflags/#zero-flags).
const EMPTY = 0;
/// The whole window requires redrawing
///
/// Note that [`event::EventMgr::redraw`] can instead be used for more
Expand All @@ -48,14 +44,24 @@ bitflags! {
/// Resize all widgets in the window
const RESIZE = 1 << 9;
/// Update theme memory
#[cfg_attr(not(feature = "internal_doc"), doc(hidden))]
#[cfg_attr(doc_cfg, doc(cfg(internal_doc)))]
const THEME_UPDATE = 1 << 10;
/// Reload per-window cache of event configuration
#[cfg_attr(not(feature = "internal_doc"), doc(hidden))]
#[cfg_attr(doc_cfg, doc(cfg(internal_doc)))]
const EVENT_CONFIG = 1 << 11;
/// Reconfigure all widgets of the window
///
/// *Configuring* widgets assigns [`WidgetId`] identifiers and calls
/// [`crate::Events::configure`].
///
/// [`WidgetId`]: crate::WidgetId
const RECONFIGURE = 1 << 16;
/// Update all widgets
///
/// This is a notification that input data has changed.
const UPDATE = 1 << 17;
/// The current window should be closed
const CLOSE = 1 << 30;
/// Close all windows and exit
Expand Down
88 changes: 53 additions & 35 deletions crates/kas-core/src/core/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
//! Widget method implementations

use crate::event::{ConfigMgr, Event, EventMgr, Response};
use crate::util::IdentifyWidget;
use crate::{Erased, Events, Layout, NavAdvance, Widget, WidgetId};
#[cfg(debug_assertions)] use crate::util::IdentifyWidget;
use crate::{Erased, Events, Layout, NavAdvance, NodeMut, Widget, WidgetId};

/// Generic implementation of [`Widget::_configure`]
pub fn _configure<W: Widget + Events<Data = <W as Widget>::Data>>(
Expand All @@ -21,29 +21,28 @@ pub fn _configure<W: Widget + Events<Data = <W as Widget>::Data>>(
for index in 0..widget.num_children() {
let id = widget.make_child_id(index);
if id.is_valid() {
if let Some(mut widget) = widget.get_child_mut(data, index) {
widget._configure(cx, id);
}
widget
.as_node_mut(data)
.for_child(index, |mut node| node._configure(cx, id));
}
}

widget.configure(data, cx);
widget.configure(cx);
widget.update(data, cx);
}

/// Generic implementation of [`Widget::_broadcast`]
pub fn _broadcast<W: Widget + Events<Data = <W as Widget>::Data>>(
/// Generic implementation of [`Widget::_update`]
pub fn _update<W: Widget + Events<Data = <W as Widget>::Data>>(
widget: &mut W,
data: &<W as Widget>::Data,
cx: &mut EventMgr,
count: &mut usize,
event: Event,
cx: &mut ConfigMgr,
) {
widget.handle_event(data, cx, event.clone());
*count += 1;
for index in 0..widget.num_children() {
if let Some(mut w) = widget.get_child_mut(data, index) {
w._broadcast(cx, count, event.clone());
}
widget.update(data, cx);
let start = cx.recurse_start.take().unwrap_or(0);
let end = cx.recurse_end.take().unwrap_or(widget.num_children());
let mut node = widget.as_node_mut(data);
for index in start..end {
node.for_child(index, |mut node| node._update(cx));
}
}

Expand All @@ -69,8 +68,13 @@ pub fn _send<W: Widget + Events<Data = <W as Widget>::Data>>(
cx.assert_post_steal_unused();
if let Some(index) = widget.find_child_index(&id) {
let translation = widget.translation();
if let Some(mut w) = widget.get_child_mut(data, index) {
response = w._send(cx, id, disabled, event.clone() + translation);
let mut found = false;
widget.as_node_mut(data).for_child(index, |mut node| {
response = node._send(cx, id.clone(), disabled, event.clone() + translation);
found = true;
});

if found {
if let Some(scroll) = cx.post_send(index) {
widget.handle_scroll(data, cx, scroll);
}
Expand All @@ -89,7 +93,7 @@ pub fn _send<W: Widget + Events<Data = <W as Widget>::Data>>(
}

if cx.has_msg() {
widget.handle_message(data, cx);
widget.handle_messages(data, cx);
}

response
Expand All @@ -104,25 +108,30 @@ pub fn _replay<W: Widget + Events<Data = <W as Widget>::Data>>(
msg: Erased,
) {
if let Some(index) = widget.find_child_index(&id) {
if let Some(mut w) = widget.get_child_mut(data, index) {
w._replay(cx, id, msg);
let mut found = false;
widget.as_node_mut(data).for_child(index, |mut node| {
node._replay(cx, id.clone(), msg);
found = true;
});

if found {
if let Some(scroll) = cx.post_send(index) {
widget.handle_scroll(data, cx, scroll);
}

if cx.has_msg() {
widget.handle_messages(data, cx);
}
} else {
#[cfg(debug_assertions)]
log::warn!(
"_replay: {} found index {index} for {id} but not child",
IdentifyWidget(widget.widget_name(), widget.id_ref())
);
}

if cx.has_msg() {
widget.handle_message(data, cx);
}
} else if id == widget.id_ref() {
cx.push_erased(msg);
widget.handle_message(data, cx);
widget.handle_messages(data, cx);
} else {
#[cfg(debug_assertions)]
log::debug!(
Expand All @@ -139,6 +148,17 @@ pub fn _nav_next<W: Widget + Events<Data = <W as Widget>::Data>>(
cx: &mut EventMgr,
focus: Option<&WidgetId>,
advance: NavAdvance,
) -> Option<WidgetId> {
let navigable = widget.navigable();
nav_next(widget.as_node_mut(data), cx, focus, advance, navigable)
}

fn nav_next(
mut widget: NodeMut<'_>,
cx: &mut EventMgr,
focus: Option<&WidgetId>,
advance: NavAdvance,
navigable: bool,
) -> Option<WidgetId> {
if cx.is_disabled(widget.id_ref()) {
return None;
Expand All @@ -147,15 +167,14 @@ pub fn _nav_next<W: Widget + Events<Data = <W as Widget>::Data>>(
let mut child = focus.and_then(|id| widget.find_child_index(id));

if let Some(index) = child {
if let Some(id) = widget
.get_child_mut(data, index)
.and_then(|mut w| w._nav_next(cx, focus, advance))
if let Some(Some(id)) =
widget.for_child(index, |mut node| node._nav_next(cx, focus, advance))
{
return Some(id);
}
}

if widget.navigable() {
if navigable {
let can_match_self = match advance {
NavAdvance::None => true,
NavAdvance::Forward(true) => true,
Expand All @@ -174,16 +193,15 @@ pub fn _nav_next<W: Widget + Events<Data = <W as Widget>::Data>>(
};

while let Some(index) = widget.nav_next(rev, child) {
if let Some(id) = widget
.get_child_mut(data, index)
.and_then(|mut w| w._nav_next(cx, focus, advance))
if let Some(Some(id)) =
widget.for_child(index, |mut node| node._nav_next(cx, focus, advance))
{
return Some(id);
}
child = Some(index);
}

if widget.navigable() {
if navigable {
let can_match_self = match advance {
NavAdvance::Reverse(true) => true,
NavAdvance::Reverse(false) => *widget.id_ref() != focus,
Expand Down
Loading

0 comments on commit d73d0b3

Please sign in to comment.