init
This commit is contained in:
72
src/main.rs
Normal file
72
src/main.rs
Normal file
@@ -0,0 +1,72 @@
|
||||
// Copyright 2019 The Druid Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
use druid::widget::{Button, Flex, Label};
|
||||
use druid::{
|
||||
AppLauncher, Data, Env, FontDescriptor, FontFamily, Lens, LocalizedString, UnitPoint, Widget,
|
||||
WidgetExt, WindowDesc,
|
||||
};
|
||||
|
||||
const VERTICAL_WIDGET_SPACING: f64 = 20.0;
|
||||
const WINDOW_TITLE: LocalizedString<HelloState> = LocalizedString::new("Hello World!");
|
||||
|
||||
#[derive(Clone, Data, Lens)]
|
||||
struct HelloState {
|
||||
value: u8,
|
||||
}
|
||||
|
||||
pub fn main() {
|
||||
// describe the main window
|
||||
let main_window = WindowDesc::new(build_root_widget)
|
||||
.title(WINDOW_TITLE)
|
||||
.window_size((400.0, 400.0));
|
||||
|
||||
// create the initial app state
|
||||
let initial_state = HelloState {
|
||||
value: 0,
|
||||
};
|
||||
|
||||
// start the application
|
||||
AppLauncher::with_window(main_window)
|
||||
.launch(initial_state)
|
||||
.expect("Failed to launch application");
|
||||
}
|
||||
|
||||
fn build_root_widget() -> impl Widget<HelloState> {
|
||||
// a label that will determine its text based on the current app data.
|
||||
let label = Label::new(|data: &HelloState, _env: &Env| {
|
||||
format!("{} %", data.value)
|
||||
})
|
||||
.with_font(FontDescriptor::new(FontFamily::SERIF).with_size(32.0))
|
||||
.align_horizontal(UnitPoint::CENTER);
|
||||
|
||||
let button_inc = Button::new("inc")
|
||||
.on_click(|_ctx, data: &mut HelloState, _env| data.value += 1)
|
||||
.padding(5.0);
|
||||
|
||||
let button_dec = Button::new("dec")
|
||||
.on_click(|_ctx, data: &mut HelloState, _env| data.value -= 1)
|
||||
.padding(5.0);
|
||||
|
||||
let inc_dec_row = Flex::row()
|
||||
.with_child(button_inc)
|
||||
.with_child(button_dec)
|
||||
.align_horizontal(UnitPoint::CENTER);
|
||||
|
||||
Flex::column()
|
||||
.with_child(label)
|
||||
.with_spacer(VERTICAL_WIDGET_SPACING)
|
||||
.with_child(inc_dec_row)
|
||||
.align_vertical(UnitPoint::CENTER)
|
||||
}
|
||||
Reference in New Issue
Block a user