simplestacknavigation/src/counter/counter_screen.rs

162 lines
5.2 KiB
Rust
Raw Normal View History

2023-12-12 12:44:16 +00:00
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::*;
2023-12-13 00:53:09 +00:00
import crate::shared::helpers::*;
2023-12-12 12:44:16 +00:00
import crate::shared::helpers::Divider;
import crate::shared::styles::*;
import crate::shared::clickable_view::ClickableView
2023-12-12 17:45:11 +00:00
import crate::shared::search_bar::SearchBar;
import crate::shared::custom_button::CustomButton;
2023-12-12 12:44:16 +00:00
2023-12-13 00:53:09 +00:00
IMG_DEFAULT_AVATAR = dep("crate://self/resources/img/default_avatar.png")
IMG_FAVORITES = dep("crate://self/resources/img/favorites.png")
IMG_MY_POSTS = dep("crate://self/resources/img/my-posts.png")
IMG_STICKER_GALLERY = dep("crate://self/resources/img/sticker-gallery.png")
IMG_SETTINGS = dep("crate://self/resources/img/settings.png")
IMG_QR = dep("crate://self/resources/img/qr_icon.png")
2023-12-12 12:44:16 +00:00
Counter = {{Counter}} {
view: {
width: Fill, height: Fit
2023-12-13 00:53:09 +00:00
spacing: 10.
2023-12-12 12:44:16 +00:00
draw_bg: {
2023-12-13 00:53:09 +00:00
color: #000
2023-12-12 12:44:16 +00:00
}
2023-12-13 00:53:09 +00:00
CounterInfo = <View> {
width: Fill, height: Fill
flow: Right,
spacing: 10.,
2023-12-12 12:44:16 +00:00
align: {
x: 0.5,
y: 0.5
},
2023-12-13 00:53:09 +00:00
show_bg: true
draw_bg: {
color: #fff
2023-12-12 12:44:16 +00:00
}
2023-12-13 00:53:09 +00:00
<View> {
width: Fit
height: Fill
2023-12-12 17:45:11 +00:00
show_bg: false
2023-12-13 00:53:09 +00:00
align: {x: 0.5, y: 0.5}
container = <RoundedView> {
width: 360
height: 400
align: {x: 0.5, y: 0.5}
spacing: 2.0
2023-12-12 17:45:11 +00:00
draw_bg: {
2023-12-13 00:53:09 +00:00
color: #ddd,
radius: (CARD_CORNER_RADIUS)
}
<View> {
width: Fit, height: Fill
flow: Down,
align: {x: 0.5, y: 0.5},
padding: {top: 5., left: 10., right: 10.}, spacing: 20.
avatar = <Image> {
source: (LOGO_MAKEPAD),
width: 300., height: 80.
}
button1 = <CustomButton> {
button = {
text: "Hello world"
}
}
message_input = <SearchBar> {
show_bg: false
input = {
width: Fill, height: Fit, margin: 0
empty_message: "Click to count"
draw_bg: {
color: #fff
border_width: 0.5,
border_color: #b4b4b4,
}
}
}
label1 = <Label> {
draw_text: {
color: #000
},
text: "Counter: 0"
}
2023-12-12 17:45:11 +00:00
}
}
2023-12-12 12:44:16 +00:00
}
2023-12-13 00:53:09 +00:00
2023-12-12 12:44:16 +00:00
}
}
}
2023-12-13 00:53:09 +00:00
2023-12-12 12:44:16 +00:00
CounterScreen = <View> {
width: Fill, height: Fill
<Counter> {}
}
}
#[derive(Live)]
pub struct Counter {
2023-12-13 00:53:09 +00:00
#[live]
view:View,
2023-12-12 12:44:16 +00:00
#[rust] counter: usize,
}
impl LiveHook for Counter {
fn before_live_design(cx: &mut Cx) {
register_widget!(cx, Counter);
}
}
impl Widget for Counter {
2023-12-13 00:53:09 +00:00
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));
});
}
2023-12-12 12:44:16 +00:00
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,
2023-12-13 00:53:09 +00:00
_dispatch_action: &mut dyn FnMut(&mut Cx, StackViewAction),
2023-12-12 12:44:16 +00:00
) {
let actions = self.view.handle_widget_event(cx, event);
2023-12-13 00:53:09 +00:00
if self.view.button(id!(button1)).clicked(&actions)
2023-12-12 12:44:16 +00:00
{
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));
}
}
}