custom button

This commit is contained in:
aOK 2023-12-12 20:45:11 +03:00
parent b7b73490c5
commit fd005e4d97
12 changed files with 378 additions and 45 deletions

View File

@ -31,4 +31,8 @@ cargo makepad check all
cargo makepad wasm install-toolchain
cargo makepad apple ios install-toolchain
cargo makepad android --abi=all install-toolchain
cargo makepad android --abi=all install-toolchain
git add .
git commit -m "welcome screen centered"
git push -u origin main

View File

@ -9,6 +9,7 @@ live_design!{
import crate::welcome::welcome_screen::WelcomeScreen
import crate::counter::counter_screen::CounterScreen
import crate::counter::counterscrn::CounterScrn
import crate::shared::clickable_view::ClickableView
import crate::shared::stack_navigation::*;
@ -41,15 +42,17 @@ live_design!{
window: {
position: vec2(0, 0),
// inner_size: vec2(360, 800)
dpi_override: 2
},
show_bg: true
width: Fill,
height: Fill
draw_bg: {
fn pixel(self) -> vec4 {
return mix(#7, #3, self.pos.y);
}
}
pass: {clear_color: #2A}
// show_bg: true
// width: Fill,
// height: Fill
// draw_bg: {
// fn pixel(self) -> vec4 {
// return mix(#7, #3, self.pos.y);
// }
// }
body = {
navigation = <StackNavigation> {
root_view = {
@ -62,6 +65,7 @@ live_design!{
padding: 0.0
welcome_frame = <WelcomeScreen> { visible: true }
// welcome_frame = <CounterScreen> { visible: true }
}
}
my_counter_stack_view = <StackNavigationView> {
@ -74,7 +78,8 @@ live_design!{
}
}
}
<CounterScreen> {}
// <CounterScreen> {}
<CounterScrn> {}
}
}
}
@ -99,6 +104,7 @@ impl LiveHook for App {
crate::welcome::welcome_screen::live_design(cx);
// counter
crate::counter::counter_screen::live_design(cx);
crate::counter::counterscrn::live_design(cx);
// shared
crate::shared::styles::live_design(cx);
@ -106,6 +112,7 @@ impl LiveHook for App {
crate::shared::header::live_design(cx);
crate::shared::search_bar::live_design(cx);
crate::shared::custom_button::live_design(cx);
crate::shared::cbutton::live_design(cx);
crate::shared::popup_menu::live_design(cx);
crate::shared::dropdown_menu::live_design(cx);
crate::shared::stack_navigation::live_design(cx);

View File

@ -14,6 +14,9 @@ live_design! {
import crate::shared::clickable_view::ClickableView
// import crate::shared::header::HeaderDropDownMenu;
import crate::shared::search_bar::SearchBar;
import crate::shared::custom_button::CustomButton;
import crate::shared::cbutton::CButton;
Counter = {{Counter}} {
@ -40,14 +43,34 @@ live_design! {
source: (LOGO_MAKEPAD),
width: 300., height: 80.
}
button1 = <Button> {
button1 = <CButton> {
button = {
width: Fit, height: Fit, margin: 0
text: "Hello world"
// draw_bg: {
// border_radius: 0.
// }
}
}
input1 = <TextInput> {
width: 100, height: 30
text: "Click to count"
// button1 = <Button> {
// text: "Hello world"
// }
// input1 = <TextInput> {
// width: 100, height: 30
// text: "Click to count"
// }
input1 = <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: #f
@ -68,9 +91,7 @@ live_design! {
#[derive(Live)]
pub struct Counter {
#[live]
view:View,
// #[live] ui: WidgetRef,
#[live] view:View,
#[rust] counter: usize,
}
@ -82,19 +103,17 @@ impl LiveHook for 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 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);
@ -114,23 +133,40 @@ impl Counter {
&mut self,
cx: &mut Cx,
event: &Event,
dispatch_action: &mut dyn FnMut(&mut Cx, StackViewAction),
// 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)
// if self
// .view
// .clickable_view(id!(my_profile_frame))
// .clicked(&actions)
// {
// dispatch_action(cx, StackViewAction::ShowCounterScreen);
// }
// if self
// .view
// .clickable_view(id!(button1.button))
// .clicked(&actions)
// {
// log!("BUTTON CLICKED {}", self.counter);
// self.counter += 1;
// let label = self.view.clickable_view(id!(label1));
// label.set_text_and_redraw(cx,&format!("Counter: {}", self.counter));
// }
if self.view.button(id!(button1.button)).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));
}
}
async fn _do_network_request(_cx:CxRef, _ui:WidgetRef, _url:&str)->String{
"".to_string()
}
}

202
src/counter/counterscrn.rs Normal file
View File

@ -0,0 +1,202 @@
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::*;
import crate::shared::helpers::Divider;
import crate::shared::styles::*;
import crate::shared::clickable_view::ClickableView
import crate::shared::search_bar::SearchBar;
import crate::shared::custom_button::CustomButton;
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")
Counter = {{Counter}} {
view: {
width: Fill, height: Fit
spacing: 10.
draw_bg: {
color: #000
}
CounterInfo = <View> {
width: Fill, height: Fill
flow: Right,
spacing: 10.,
// padding: {top: 100., bottom: 100., right: 10., left: 10.}
align: {
x: 0.5,
y: 0.5
},
show_bg: true
draw_bg: {
color: #fff
}
<View> {
width: Fit
height: Fill
show_bg: false
// align:{ x: 0.5 }
align: {x: 0.5, y: 0.5}
container = <RoundedView> {
width: 360
height: 400
// flow: Down
align: {x: 0.5, y: 0.5}
// margin: {left: 200.0, right: 200.0, top: 100.0, bottom: 100.0}
spacing: 2.0
draw_bg: {
color: #ddd,
// border_width: 1.0,
// border_color: #000,
radius: (CARD_CORNER_RADIUS)
}
<View> {
width: Fit, height: Fill
flow: Down,
align: {x: 0, y: 0.5},
padding: {top: 5., left: 10., right: 10.}, spacing: 20.
// username = <Label> {
// // padding: {top: 50., left: 140.}
// draw_text:{
// color: #000,
// text_style: <TEXT_SUB>{font_size: 20.},
// }
// text:"Welcome To Simple Nav"
// }
avatar = <Image> {
source: (LOGO_MAKEPAD),
width: 300., height: 80.
}
button1 = <CustomButton> {
button = {
// width: Fill, height: Fit, margin: 0
text: "Hello world"
}
}
// button1 = <Button> {
// text: "Hello world"
// }
// input1 = <TextInput> {
// width: 100, height: 30
// text: "Click to count"
// }
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> {
width: Fill, height: Fit, margin: 0
draw_text: {
color: #000
},
text: "Counter: 0"
}
}
}
}
}
}
}
CounterScrn = <View> {
width: Fill, height: Fill
<Counter> {}
}
}
#[derive(Live)]
pub struct Counter {
#[live]
view:View,
#[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!(welcome_frame))
// .clicked(&actions)
// {
// dispatch_action(cx, StackViewAction::ShowCounterScreen);
// log!("WELCOME BUTTON CLICKED");
// }
if self
.view
.clickable_view(id!(button1.button))
.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));
}
// if self.view.button(id!(button1.button)).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));
// }
}
}

View File

@ -1 +1,2 @@
pub mod counter_screen;
pub mod counter_screen;
pub mod counterscrn;

56
src/shared/cbutton.rs Normal file
View File

@ -0,0 +1,56 @@
use makepad_widgets::*;
live_design! {
import makepad_draw::shader::std::*;
import makepad_widgets::base::*;
import makepad_widgets::theme_desktop_dark::*;
import crate::shared::styles::*;
import crate::shared::clickable_view::ClickableView
CButton = <View> {
width: Fill, height: Fit
// flow: Right
show_bg: false
draw_bg: {
// color: #EDEDED;
color: #000;
}
button = <Button> {
width: Fill, height: 34.
text: "Welcome"
draw_text: {
text_style: <REGULAR_TEXT>{font_size: 12.},
fn get_color(self) -> vec4 {
return #000
}
}
draw_bg: {
border_radius: 8.
fn pixel(self) -> vec4 {
let border_color = #b4b4b4;
let border_width = 0.5;
let sdf = Sdf2d::viewport(self.pos * self.rect_size);
let body = mix(mix(#f, #f0, self.hover), #d1, self.pressed);
sdf.box(
1.,
1.,
self.rect_size.x - 2.0,
self.rect_size.y - 2.0,
self.border_radius
)
sdf.fill_keep(body)
sdf.stroke(
border_color,
border_width
)
return sdf.result
}
}
}
}
}

View File

@ -25,7 +25,7 @@ live_design! {
title = <Label> {
width: Fit, height: Fit
draw_text: {
color: #fff,
color: #000,
text_style: <TITLE_TEXT>{},
},
text: "微信"

View File

@ -6,5 +6,6 @@ pub mod popup_menu;
pub mod header;
pub mod search_bar;
pub mod custom_button;
pub mod cbutton;
pub mod helpers;
pub mod styles;

View File

@ -20,6 +20,7 @@ live_design! {
clip_y: true,
align: {y: 0.5}
empty_message: "Search"
// text: "Here"
draw_bg: {
color: #fff
}

View File

@ -49,7 +49,8 @@ live_design! {
ease: ExpDecay {d1: 0.80, d2: 0.97}
from: {all: Forward {duration: 0.3}}
// Bug: Constants are not working as part of an live state value
apply: {offset: 400.0}
// apply: {offset: 400.0}
apply: {offset: 0.0}
}
show = {

View File

@ -3,7 +3,7 @@ use makepad_widgets::*;
live_design! {
TITLE_TEXT = {
font_size: (14),
// font: {path: dep("crate://makepad-widgets/resources/GoNotoKurrent-Regular.ttf")}
font: {path: dep("crate://makepad-widgets/resources/GoNotoKurrent-Regular.ttf")}
}
REGULAR_TEXT = {
@ -21,10 +21,23 @@ live_design! {
}
LOGO_MAKEPAD = dep("crate://self/resources/makepad.png")
// LOGO_MAKEPAD = dep("crate://self/resources/makepad.svg")
CARD_CORNER_RADIUS = 20.
CARD_HEIGHT = 550
COLOR_PROFILE_CIRCLE = #xfff8ee
COLOR_DIVIDER = #x00000018
COLOR_DIVIDER_DARK = #x00000044
// COLOR_BG = #xfff8ee
// COLOR_BRAND = #xf88
// COLOR_BRAND_HOVER = #xf66
// COLOR_META_TEXT = #xaaa
// COLOR_META = #xccc
// COLOR_META_INV = #xfffa
// COLOR_OVERLAY_BG = #x000000d8
// COLOR_DIVIDER = #x00000018
// COLOR_DIVIDER_DARK = #x00000044
// COLOR_PROFILE_CIRCLE = #xfff8ee
// COLOR_P = #x999
}

View File

@ -25,9 +25,7 @@ live_design! {
Welcome = {{Welcome}} {
view: {
width: Fill, height: Fit
// flow: Down,
spacing: 10.
// show_bg: true,
draw_bg: {
color: #000
}
@ -82,6 +80,7 @@ live_design! {
input = {
width: Fill, height: Fit, margin: 0
empty_message: "Enter Your Name"
// text: ""
draw_bg: {
color: #fff
border_width: 0.5,
@ -163,6 +162,18 @@ impl Welcome {
.clicked(&actions)
{
dispatch_action(cx, StackViewAction::ShowCounterScreen);
// let input11 = self.view.text_input(id!(message_input));
let user_prompt1 = self.view.text_input(id!(message_input.input)).text();
log!("WELCOME BUTTON CLICKED: {}", user_prompt1);
}
// 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));
// }
}
}