Skip to content

Commit

Permalink
Split the View trait into View and Widget (#288)
Browse files Browse the repository at this point in the history
  • Loading branch information
Zoxc authored Jan 28, 2024
1 parent 9c88792 commit 1c417d0
Show file tree
Hide file tree
Showing 39 changed files with 824 additions and 459 deletions.
4 changes: 2 additions & 2 deletions examples/dyn-container/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ fn app_view() -> impl View {
dyn_container(
move || view.get(),
move |value| match value {
ViewSwitcher::One => Box::new(view_one()),
ViewSwitcher::Two => Box::new(view_two(view)),
ViewSwitcher::One => view_one().any(),
ViewSwitcher::Two => view_two(view).any(),
},
)
.style(|s| s.padding(10).border(1)),
Expand Down
6 changes: 3 additions & 3 deletions examples/flight_booker/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@ pub fn app_view() -> impl View {
let success_message = dyn_container(
move || did_booking.get(),
move |booked| match (booked, flight_mode.get()) {
(true, FlightMode::OneWay) => Box::new(text(oneway_message(start_text.get()))),
(true, FlightMode::OneWay) => text(oneway_message(start_text.get())).any(),
(true, FlightMode::Return) => {
Box::new(text(return_message(start_text.get(), return_text.get())))
text(return_message(start_text.get(), return_text.get())).any()
}
(false, _) => Box::new(empty()),
(false, _) => empty().any(),
},
);

Expand Down
7 changes: 5 additions & 2 deletions src/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::{
id::Id,
menu::Menu,
update::{UpdateMessage, CENTRAL_UPDATE_MESSAGES},
view::View,
view::Widget,
window_handle::{get_current_view, set_current_view},
};

Expand Down Expand Up @@ -165,7 +165,10 @@ pub fn set_ime_cursor_area(position: Point, size: Size) {
}

/// Creates a new overlay on the current window.
pub fn add_overlay<V: View + 'static>(position: Point, view: impl FnOnce(Id) -> V + 'static) -> Id {
pub fn add_overlay<V: Widget + 'static>(
position: Point,
view: impl FnOnce(Id) -> V + 'static,
) -> Id {
let id = Id::next();
add_update_message(UpdateMessage::AddOverlay {
id,
Expand Down
13 changes: 9 additions & 4 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@ use once_cell::sync::Lazy;
use parking_lot::Mutex;

use crate::{
action::Timer, app_handle::ApplicationHandle, clipboard::Clipboard, inspector::Capture,
profiler::Profile, view::View, window::WindowConfig,
action::Timer,
app_handle::ApplicationHandle,
clipboard::Clipboard,
inspector::Capture,
profiler::Profile,
view::{AnyView, View},
window::WindowConfig,
};

use raw_window_handle::HasRawDisplayHandle;
Expand Down Expand Up @@ -42,7 +47,7 @@ pub(crate) enum UserEvent {

pub(crate) enum AppUpdateEvent {
NewWindow {
view_fn: Box<dyn FnOnce(WindowId) -> Box<dyn View>>,
view_fn: Box<dyn FnOnce(WindowId) -> AnyView>,
config: Option<WindowConfig>,
},
CloseWindow {
Expand Down Expand Up @@ -121,7 +126,7 @@ impl Application {
) -> Self {
self.handle.as_mut().unwrap().new_window(
&self.event_loop,
Box::new(|window_id| Box::new(app_view(window_id))),
Box::new(|window_id| app_view(window_id).any()),
config,
);
self
Expand Down
4 changes: 2 additions & 2 deletions src/app_handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::{
ext_event::EXT_EVENT_HANDLER,
inspector::Capture,
profiler::{Profile, ProfileEvent},
view::View,
view::AnyView,
window::WindowConfig,
window_handle::WindowHandle,
};
Expand Down Expand Up @@ -239,7 +239,7 @@ impl ApplicationHandle {
pub(crate) fn new_window(
&mut self,
event_loop: &EventLoopWindowTarget<UserEvent>,
view_fn: Box<dyn FnOnce(WindowId) -> Box<dyn View>>,
view_fn: Box<dyn FnOnce(WindowId) -> AnyView>,
config: Option<WindowConfig>,
) {
let mut window_builder = floem_winit::window::WindowBuilder::new();
Expand Down
36 changes: 18 additions & 18 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use crate::{
ZIndex,
},
unit::PxPct,
view::{paint_bg, paint_border, paint_outline, View, ViewData},
view::{paint_bg, paint_border, paint_outline, ViewData, Widget},
view_data::ChangeFlags,
};

Expand Down Expand Up @@ -172,12 +172,12 @@ impl AppState {
}

/// This removes a view from the app state.
pub fn remove_view(&mut self, view: &mut dyn View) {
pub fn remove_view(&mut self, view: &mut dyn Widget) {
view.for_each_child_mut(&mut |child| {
self.remove_view(child);
false
});
let id = view.id();
let id = view.view_data().id();
let view_state = self.view_state(id);
if let Some(action) = view_state.cleanup_listener.as_ref() {
action();
Expand Down Expand Up @@ -597,11 +597,11 @@ impl<'a> EventCx<'a> {
/// Internal method used by Floem. This can be called from parent `View`s to propagate an event to the child `View`.
pub fn view_event(
&mut self,
view: &mut dyn View,
view: &mut dyn Widget,
id_path: Option<&[Id]>,
event: Event,
) -> EventPropagation {
if self.should_send(view.id(), &event) {
if self.should_send(view.view_data().id(), &event) {
self.unconditional_view_event(view, id_path, event)
} else {
EventPropagation::Continue
Expand All @@ -611,11 +611,11 @@ impl<'a> EventCx<'a> {
/// Internal method used by Floem. This can be called from parent `View`s to propagate an event to the child `View`.
pub(crate) fn unconditional_view_event(
&mut self,
view: &mut dyn View,
view: &mut dyn Widget,
id_path: Option<&[Id]>,
event: Event,
) -> EventPropagation {
let id = view.id();
let id = view.view_data().id();
if self.app_state.is_hidden(id) {
// we don't process events for hidden view
return EventPropagation::Continue;
Expand Down Expand Up @@ -645,7 +645,7 @@ impl<'a> EventCx<'a> {
let id = id_path[0];
let id_path = &id_path[1..];

if id != view.id() {
if id != view.view_data().id() {
// This shouldn't happen
return EventPropagation::Continue;
}
Expand Down Expand Up @@ -1050,9 +1050,9 @@ impl<'a> StyleCx<'a> {
}

/// Internal method used by Floem to compute the styles for the view.
pub fn style_view(&mut self, view: &mut dyn View) {
pub fn style_view(&mut self, view: &mut dyn Widget) {
self.save();
let id = view.id();
let id = view.view_data().id();
let view_state = self.app_state_mut().view_state(id);
if !view_state.requested_changes.contains(ChangeFlags::STYLE) {
return;
Expand All @@ -1074,7 +1074,7 @@ impl<'a> StyleCx<'a> {
if view_state.request_style_recursive {
view_state.request_style_recursive = false;
view.for_each_child(&mut |child| {
let state = self.app_state_mut().view_state(child.id());
let state = self.app_state_mut().view_state(child.view_data().id());
state.request_style_recursive = true;
state.requested_changes.insert(ChangeFlags::STYLE);
false
Expand Down Expand Up @@ -1255,8 +1255,8 @@ impl<'a> ComputeLayoutCx<'a> {
/// - invoking any attached context::ResizeListeners
///
/// Returns the bounding rect that encompasses this view and its children
pub fn compute_view_layout(&mut self, view: &mut dyn View) -> Option<Rect> {
let id = view.id();
pub fn compute_view_layout(&mut self, view: &mut dyn Widget) -> Option<Rect> {
let id = view.view_data().id();
if self.app_state().is_hidden(id) {
self.app_state_mut().view_state(id).layout_rect = Rect::ZERO;
return None;
Expand Down Expand Up @@ -1381,7 +1381,7 @@ impl<'a> LayoutCx<'a> {
}

/// Internal method used by Floem to invoke the user-defined `View::layout` method.
pub fn layout_view(&mut self, view: &mut dyn View) -> Node {
pub fn layout_view(&mut self, view: &mut dyn Widget) -> Node {
view.layout(self)
}
}
Expand Down Expand Up @@ -1427,8 +1427,8 @@ impl<'a> PaintCx<'a> {
/// - managing hidden status
/// - clipping
/// - painting computed styles like background color, border, font-styles, and z-index and handling painting requirements of drag and drop
pub fn paint_view(&mut self, view: &mut dyn View) {
let id = view.id();
pub fn paint_view(&mut self, view: &mut dyn Widget) {
let id = view.view_data().id();
if self.app_state.is_hidden(id) {
return;
}
Expand Down Expand Up @@ -1674,10 +1674,10 @@ impl<'a> UpdateCx<'a> {

/// Used internally by Floem to send an update to the correct view based on the `Id` path.
/// It will invoke only once `update` when the correct view is located.
pub fn update_view(&mut self, view: &mut dyn View, id_path: &[Id], state: Box<dyn Any>) {
pub fn update_view(&mut self, view: &mut dyn Widget, id_path: &[Id], state: Box<dyn Any>) {
let id = id_path[0];
let id_path = &id_path[1..];
if id == view.id() {
if id == view.view_data().id() {
if id_path.is_empty() {
view.update(self, state);
} else if let Some(child) = view.child_mut(id_path[0]) {
Expand Down
Loading

0 comments on commit 1c417d0

Please sign in to comment.