#![windows_subsystem = "windows"]
mod cmd; use std::time::Instant; use libloader::libloading; use druid::widget::prelude::*; use druid::text::{FontDescriptor,FontFamily}; use druid::{AppLauncher,Color,Data,FileDialogOptions,FileSpec,Lens,Menu,MenuItem,Point,TextAlignment,UnitPoint,WidgetExt,WindowDesc}; use druid::widget::{Either,Flex,Label,Button,Split,Tabs,TextBox,Controller,TabsTransition,MainAxisAlignment,CrossAxisAlignment,Painter,LineBreaking,Scroll,ViewSwitcher};#[derive(Clone, Data, Lens)] struct AppState { name: String, show_hide: bool, sidebar_view: u8, } struct MainMenuController; struct IconColorController0; struct IconColorController1; struct MessageBoxTextController; struct MessageBoxFocusController;
impl <W:Widget<AppState>> Controller<AppState, W> for MainMenuController { fn event(&mut self, child: &mut W, ctx: &mut druid::EventCtx, event: &druid::Event, data: &mut AppState, env: &druid::Env) { if let Event::MouseDown(e) = event { if e.button.is_left() && ctx.is_hot(){ctx.show_context_menu(make_main_menu(),Point::new(40., 0.));} } child.event(ctx, event, data, env) } } impl Controller<AppState, Label<AppState>> for IconColorController0 { fn update(&mut self,child: &mut Label<AppState>,ctx: &mut UpdateCtx,old_data: &AppState,data: &AppState,env: &Env,) { if data.show_hide == true && data.sidebar_view == 0{ child.set_text_color(Color::rgb8(0, 255, 255));//青色 ctx.request_layout(); } else { child.set_text_color(Color::WHITE); ctx.request_layout(); } child.update(ctx, old_data, data, env); } } impl Controller<AppState, Label<AppState>> for IconColorController1 { fn update(&mut self,child: &mut Label<AppState>,ctx: &mut UpdateCtx,old_data: &AppState,data: &AppState,env: &Env,) { if data.show_hide == true && data.sidebar_view == 1{ child.set_text_color(Color::rgb8(0, 255, 255));//青色 ctx.request_layout(); } else { child.set_text_color(Color::WHITE); ctx.request_layout(); } child.update(ctx, old_data, data, env); } } impl <W:Widget<AppState>> Controller<AppState, W> for MessageBoxTextController { fn event(&mut self, child: &mut W, ctx: &mut druid::EventCtx, event: &druid::Event, data: &mut AppState, env: &druid::Env) { if let Event::MouseDown(e) = event { if e.button == druid::MouseButton::Left {data.name = String::from("正在爬取沪深京A股数据请稍后······");} } child.event(ctx, event, data, env) } } impl <W:Widget<AppState>> Controller<AppState, W> for MessageBoxFocusController { fn event(&mut self, child: &mut W, ctx: &mut druid::EventCtx, event: &druid::Event, data: &mut AppState, env: &druid::Env) { if let Event::MouseDown(e) = event { if e.button == druid::MouseButton::Left {ctx.request_focus();} } child.event(ctx, event, data, env) } } fn ui_builder() -> impl Widget<AppState> { let main_window: Split<AppState> = Split::columns(sidebar(), tab()).draggable(true).solid_bar(true).split_point(0.1).bar_size(0.0).min_bar_area(0.1); let main_window: Either<AppState> = Either::new(|data:&AppState, _env:&Env| data.show_hide,main_window,tab(),); Flex::row().with_child(toolbar()).with_flex_child(main_window,1.0) } fn main() { let initial_state:AppState = AppState { name: "".to_string(), show_hide: true, sidebar_view: 0, }; let main_window: WindowDesc<AppState> = WindowDesc::new(ui_builder()).title("MOSS"); AppLauncher::with_window(main_window).log_to_console().launch(initial_state).expect("launch failed"); } fn toolbar_painter()-> Painter<AppState>{ Painter::new(|ctx: &mut PaintCtx, _, _env: &Env| { let bounds: druid::kurbo::RoundedRect = ctx.size().to_rect().to_rounded_rect(5.); if ctx.is_hot() { ctx.fill(bounds, &Color::rgb8(70, 70, 70));//亮灰色 } }) } fn toolbar()-> impl Widget<AppState>{ let menu: druid::widget::ControllerHost<druid::widget::Align<AppState>, MainMenuController> = Label::new(" ☰") .with_text_size(17.) .fix_width(30.) .background(toolbar_painter()) .on_click(|_,_,_|{todo!()}) .center() .controller(MainMenuController{}); let sidebar_view0 = Label::new(" 标签:mut,ctx,Druid,let,child,new,data,Rust From: https://www.cnblogs.com/xorover/p/18217842