forked from linebender/druid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shello.rs
142 lines (121 loc) · 4.34 KB
/
shello.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Copyright 2018 The xi-editor 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 std::any::Any;
use druid_shell::kurbo::{Line, Rect, Vec2};
use druid_shell::piet::{Color, RenderContext};
use druid_shell::{
Application, Cursor, FileDialogOptions, FileSpec, HotKey, KeyEvent, KeyModifiers, Menu,
MouseEvent, RunLoop, SysMods, TimerToken, WinCtx, WinHandler, WindowBuilder, WindowHandle,
};
const BG_COLOR: Color = Color::rgb8(0x27, 0x28, 0x22);
const FG_COLOR: Color = Color::rgb8(0xf0, 0xf0, 0xea);
#[derive(Default)]
struct HelloState {
size: (f64, f64),
handle: WindowHandle,
}
impl WinHandler for HelloState {
fn connect(&mut self, handle: &WindowHandle) {
self.handle = handle.clone();
}
fn paint(&mut self, piet: &mut piet_common::Piet, _ctx: &mut dyn WinCtx) -> bool {
let (width, height) = self.size;
let rect = Rect::new(0.0, 0.0, width, height);
piet.fill(rect, &BG_COLOR);
piet.stroke(Line::new((10.0, 50.0), (90.0, 90.0)), &FG_COLOR, 1.0);
false
}
fn command(&mut self, id: u32, ctx: &mut dyn WinCtx) {
match id {
0x100 => {
self.handle.close();
Application::quit();
}
0x101 => {
let options = FileDialogOptions::new().show_hidden().allowed_types(vec![
FileSpec::new("Rust Files", &["rs", "toml"]),
FileSpec::TEXT,
FileSpec::JPG,
]);
let filename = ctx.open_file_sync(options);
println!("result: {:?}", filename);
}
_ => println!("unexpected id {}", id),
}
}
fn key_down(&mut self, event: KeyEvent, ctx: &mut dyn WinCtx) -> bool {
let deadline = std::time::Instant::now() + std::time::Duration::from_millis(500);
let id = ctx.request_timer(deadline);
println!("keydown: {:?}, timer id = {:?}", event, id);
false
}
fn wheel(&mut self, delta: Vec2, mods: KeyModifiers, _ctx: &mut dyn WinCtx) {
println!("mouse_wheel {:?} {:?}", delta, mods);
}
fn mouse_move(&mut self, event: &MouseEvent, ctx: &mut dyn WinCtx) {
ctx.set_cursor(&Cursor::Arrow);
println!("mouse_move {:?}", event);
}
fn mouse_down(&mut self, event: &MouseEvent, _ctx: &mut dyn WinCtx) {
println!("mouse_down {:?}", event);
}
fn mouse_up(&mut self, event: &MouseEvent, _ctx: &mut dyn WinCtx) {
println!("mouse_up {:?}", event);
}
fn timer(&mut self, id: TimerToken, _ctx: &mut dyn WinCtx) {
println!("timer fired: {:?}", id);
}
fn size(&mut self, width: u32, height: u32, _ctx: &mut dyn WinCtx) {
let dpi = self.handle.get_dpi();
let dpi_scale = dpi as f64 / 96.0;
let width_f = (width as f64) / dpi_scale;
let height_f = (height as f64) / dpi_scale;
self.size = (width_f, height_f);
}
fn destroy(&mut self, _ctx: &mut dyn WinCtx) {
Application::quit()
}
fn as_any(&mut self) -> &mut dyn Any {
self
}
}
fn main() {
Application::init();
let mut file_menu = Menu::new();
file_menu.add_item(
0x100,
"E&xit",
Some(&HotKey::new(SysMods::Cmd, "q")),
true,
false,
);
file_menu.add_item(
0x101,
"O&pen",
Some(&HotKey::new(SysMods::Cmd, "o")),
true,
false,
);
let mut menubar = Menu::new();
menubar.add_dropdown(Menu::new(), "Application", true);
menubar.add_dropdown(file_menu, "&File", true);
let mut run_loop = RunLoop::new();
let mut builder = WindowBuilder::new();
builder.set_handler(Box::new(HelloState::default()));
builder.set_title("Hello example");
builder.set_menu(menubar);
let window = builder.build().unwrap();
window.show();
run_loop.run();
}