simplestacknavigation/src/counter/counter_screen.rs

137 lines
3.4 KiB
Rust

use crate::shared::clickable_view::*;
use crate::shared::stack_view_action::StackViewAction;
use makepad_widgets::widget::WidgetCache;
use makepad_widgets::*;
live_design! {
import makepad_draw::shader::std::*;
import makepad_widgets::base::*;
import makepad_widgets::theme_desktop_dark::*;
import crate::shared::helpers::FillerX;
import crate::shared::helpers::Divider;
import crate::shared::styles::*;
import crate::shared::clickable_view::ClickableView
// import crate::shared::header::HeaderDropDownMenu;
Counter = {{Counter}} {
view: {
width: Fill, height: Fit
flow: Down, spacing: 10.
show_bg: true,
draw_bg: {
color: #ddd
}
// <CounterHeader> {}
body = <View>{
flow: Down,
spacing: 20,
align: {
x: 0.5,
y: 0.5
},
avatar = <Image> {
source: (LOGO_MAKEPAD),
width: 300., height: 80.
}
button1 = <Button> {
text: "Hello world"
}
input1 = <TextInput> {
width: 100, height: 30
text: "Click to count"
}
label1 = <Label> {
draw_text: {
color: #f
},
text: "Counter: 0"
}
}
}
}
CounterScreen = <View> {
width: Fill, height: Fill
<Counter> {}
}
}
#[derive(Live)]
pub struct Counter {
#[live]
view:View,
// #[live] ui: WidgetRef,
#[rust] counter: usize,
}
impl LiveHook for Counter {
fn before_live_design(cx: &mut Cx) {
register_widget!(cx, Counter);
}
}
impl Widget for Counter {
fn handle_widget_event_with(
&mut self,
cx: &mut Cx,
event: &Event,
dispatch_action: &mut dyn FnMut(&mut Cx, WidgetActionItem),
) {
let uid = self.widget_uid();
self.handle_event_with(cx, event, &mut |cx, action: StackViewAction| {
dispatch_action(cx, WidgetActionItem::new(action.into(), uid));
});
}
fn redraw(&mut self, cx: &mut Cx) {
self.view.redraw(cx);
}
fn find_widgets(&mut self, path: &[LiveId], cached: WidgetCache, results: &mut WidgetSet) {
self.view.find_widgets(path, cached, results);
}
fn draw_walk_widget(&mut self, cx: &mut Cx2d, walk: Walk) -> WidgetDraw {
self.view.draw_walk_widget(cx, walk)
}
}
impl Counter {
fn handle_event_with(
&mut self,
cx: &mut Cx,
event: &Event,
dispatch_action: &mut dyn FnMut(&mut Cx, StackViewAction),
) {
let actions = self.view.handle_widget_event(cx, event);
if self
.view
.clickable_view(id!(my_profile_frame))
.clicked(&actions)
{
dispatch_action(cx, StackViewAction::ShowMyProfile);
}
if self.view.button(id!(button1)).clicked(&actions) {
log!("BUTTON CLICKED {}", self.counter);
self.counter += 1;
let label = self.view.label(id!(label1));
label.set_text_and_redraw(cx,&format!("Counter: {}", self.counter));
}
}
}