pub struct Application { /* private fields */ }
Expand description
The top level application object.
This can be thought of as a reference and it can be safely cloned.
Implementations§
Source§impl Application
impl Application
Sourcepub fn new() -> Result<Application, Error>
pub fn new() -> Result<Application, Error>
Create a new Application
.
§Errors
Errors if an Application
has already been created.
This may change in the future. See druid#771 for discussion.
Examples found in repository?
More examples
138fn main() {
139 tracing_subscriber::fmt().init();
140 let app = Application::new().unwrap();
141 let mut builder = WindowBuilder::new(app.clone());
142 let perf_test = PerfTest {
143 size: Size::ZERO,
144 handle: Default::default(),
145 start_time: time::Instant::now(),
146 last_time: time::Instant::now(),
147 red: true,
148 };
149 builder.set_handler(Box::new(perf_test));
150 builder.set_title("Performance tester");
151
152 let window = builder.build().unwrap();
153 window.show();
154
155 app.run(None);
156}
104fn main() {
105 tracing_subscriber::fmt().init();
106 let app = Application::new().unwrap();
107 let mut builder = WindowBuilder::new(app.clone());
108 let inv_test = InvalidateTest {
109 size: Size::ZERO,
110 handle: Default::default(),
111 start_time: Instant::now(),
112 rect: Rect::from_origin_size(Point::ZERO, (10.0, 20.0)),
113 cursor: Rect::from_origin_size(Point::ZERO, (2.0, 100.0)),
114 color: Color::WHITE,
115 };
116 builder.set_handler(Box::new(inv_test));
117 builder.set_title("Invalidate tester");
118
119 let window = builder.build().unwrap();
120 window.show();
121 app.run(None);
122}
69fn main() {
70 tracing_subscriber::fmt().init();
71 let app = Application::new().unwrap();
72
73 let mut file_menu = Menu::new();
74 file_menu.add_item(
75 0x100,
76 "E&xit",
77 Some(&HotKey::new(SysMods::Cmd, "q")),
78 None,
79 true,
80 );
81
82 let mut menubar = Menu::new();
83 menubar.add_dropdown(file_menu, "Application", true);
84
85 let mut builder = WindowBuilder::new(app.clone());
86 builder.set_handler(Box::<QuitState>::default());
87 builder.set_title("Quit example");
88 builder.set_menu(menubar);
89
90 let window = builder.build().unwrap();
91 window.show();
92
93 app.run(None);
94}
136fn main() {
137 tracing_subscriber::fmt().init();
138 let mut file_menu = Menu::new();
139 file_menu.add_item(
140 0x100,
141 "E&xit",
142 Some(&HotKey::new(SysMods::Cmd, "q")),
143 None,
144 true,
145 );
146 file_menu.add_item(
147 0x101,
148 "O&pen",
149 Some(&HotKey::new(SysMods::Cmd, "o")),
150 None,
151 true,
152 );
153 file_menu.add_item(
154 0x102,
155 "S&ave",
156 Some(&HotKey::new(SysMods::Cmd, "s")),
157 None,
158 true,
159 );
160 let mut menubar = Menu::new();
161 menubar.add_dropdown(Menu::new(), "Application", true);
162 menubar.add_dropdown(file_menu, "&File", true);
163
164 let app = Application::new().unwrap();
165 let mut builder = WindowBuilder::new(app.clone());
166 builder.set_handler(Box::<HelloState>::default());
167 builder.set_title("Hello example");
168 builder.set_menu(menubar);
169
170 let window = builder.build().unwrap();
171 window.show();
172
173 app.run(None);
174}
Sourcepub fn global() -> Application
pub fn global() -> Application
Get the current globally active Application
.
A globally active Application
exists
after new
is called and until run
returns.
§Panics
Panics if there is no globally active Application
.
For a non-panicking function use try_global
.
This function will also panic if called from a non-main thread.
Examples found in repository?
More examples
119 fn command(&mut self, id: u32) {
120 match id {
121 0x100 => {
122 self.handle.close();
123 Application::global().quit()
124 }
125 _ => println!("unexpected id {id}"),
126 }
127 }
128
129 fn key_down(&mut self, event: KeyEvent) -> bool {
130 if event.key == Key::Character("c".to_string()) {
131 // custom hotkey for pressing "c"
132 println!("user pressed c! wow! setting selection to 0");
133
134 // update internal selection state
135 self.document.borrow_mut().selection = Selection::caret(0);
136
137 // notify the OS that we've updated the selection
138 self.handle
139 .update_text_field(self.text_input_token.unwrap(), Event::SelectionChanged);
140
141 // repaint window
142 self.handle.request_anim_frame();
143
144 // return true prevents the keypress event from being handled as text input
145 return true;
146 }
147 false
148 }
149
150 fn acquire_input_lock(
151 &mut self,
152 _token: TextFieldToken,
153 _mutable: bool,
154 ) -> Box<dyn InputHandler> {
155 Box::new(AppInputHandler {
156 state: self.document.clone(),
157 window_size: self.size,
158 window_handle: self.handle.clone(),
159 })
160 }
161
162 fn release_input_lock(&mut self, _token: TextFieldToken) {
163 // no action required; this example is simple enough that this
164 // state is not actually shared.
165 }
166
167 fn size(&mut self, size: Size) {
168 self.size = size;
169 }
170
171 fn request_close(&mut self) {
172 self.handle.close();
173 }
174
175 fn destroy(&mut self) {
176 Application::global().quit()
177 }
47 fn command(&mut self, id: u32) {
48 match id {
49 0x100 => {
50 self.handle.close();
51 Application::global().quit()
52 }
53 0x101 => {
54 let options = FileDialogOptions::new().show_hidden().allowed_types(vec![
55 FileSpec::new("Rust Files", &["rs", "toml"]),
56 FileSpec::TEXT,
57 FileSpec::JPG,
58 ]);
59 self.handle.open_file(options);
60 }
61 0x102 => {
62 let options = FileDialogOptions::new().show_hidden().allowed_types(vec![
63 FileSpec::new("Rust Files", &["rs", "toml"]),
64 FileSpec::TEXT,
65 FileSpec::JPG,
66 ]);
67 self.handle.save_as(options);
68 }
69 _ => println!("unexpected id {id}"),
70 }
71 }
72
73 fn open_file(&mut self, _token: FileDialogToken, file_info: Option<FileInfo>) {
74 println!("open file result: {file_info:?}");
75 }
76
77 fn save_as(&mut self, _token: FileDialogToken, file: Option<FileInfo>) {
78 println!("save file result: {file:?}");
79 }
80
81 fn key_down(&mut self, event: KeyEvent) -> bool {
82 println!("keydown: {event:?}");
83 false
84 }
85
86 fn key_up(&mut self, event: KeyEvent) {
87 println!("keyup: {event:?}");
88 }
89
90 fn wheel(&mut self, event: &MouseEvent) {
91 println!("mouse_wheel {event:?}");
92 }
93
94 fn mouse_move(&mut self, event: &MouseEvent) {
95 self.handle.set_cursor(&Cursor::Arrow);
96 println!("mouse_move {event:?}");
97 }
98
99 fn mouse_down(&mut self, event: &MouseEvent) {
100 println!("mouse_down {event:?}");
101 }
102
103 fn mouse_up(&mut self, event: &MouseEvent) {
104 println!("mouse_up {event:?}");
105 }
106
107 fn timer(&mut self, id: TimerToken) {
108 println!("timer fired: {id:?}");
109 }
110
111 fn size(&mut self, size: Size) {
112 self.size = size;
113 }
114
115 fn got_focus(&mut self) {
116 println!("Got focus");
117 }
118
119 fn lost_focus(&mut self) {
120 println!("Lost focus");
121 }
122
123 fn request_close(&mut self) {
124 self.handle.close();
125 }
126
127 fn destroy(&mut self) {
128 Application::global().quit()
129 }
Sourcepub fn try_global() -> Option<Application>
pub fn try_global() -> Option<Application>
Sourcepub fn run(self, handler: Option<Box<dyn AppHandler>>)
pub fn run(self, handler: Option<Box<dyn AppHandler>>)
Start the Application
runloop.
The provided handler
will be used to inform of events.
This will consume the Application
and block the current thread
until the Application
has finished executing.
§Panics
Panics if the Application
is already running.
Examples found in repository?
More examples
138fn main() {
139 tracing_subscriber::fmt().init();
140 let app = Application::new().unwrap();
141 let mut builder = WindowBuilder::new(app.clone());
142 let perf_test = PerfTest {
143 size: Size::ZERO,
144 handle: Default::default(),
145 start_time: time::Instant::now(),
146 last_time: time::Instant::now(),
147 red: true,
148 };
149 builder.set_handler(Box::new(perf_test));
150 builder.set_title("Performance tester");
151
152 let window = builder.build().unwrap();
153 window.show();
154
155 app.run(None);
156}
104fn main() {
105 tracing_subscriber::fmt().init();
106 let app = Application::new().unwrap();
107 let mut builder = WindowBuilder::new(app.clone());
108 let inv_test = InvalidateTest {
109 size: Size::ZERO,
110 handle: Default::default(),
111 start_time: Instant::now(),
112 rect: Rect::from_origin_size(Point::ZERO, (10.0, 20.0)),
113 cursor: Rect::from_origin_size(Point::ZERO, (2.0, 100.0)),
114 color: Color::WHITE,
115 };
116 builder.set_handler(Box::new(inv_test));
117 builder.set_title("Invalidate tester");
118
119 let window = builder.build().unwrap();
120 window.show();
121 app.run(None);
122}
69fn main() {
70 tracing_subscriber::fmt().init();
71 let app = Application::new().unwrap();
72
73 let mut file_menu = Menu::new();
74 file_menu.add_item(
75 0x100,
76 "E&xit",
77 Some(&HotKey::new(SysMods::Cmd, "q")),
78 None,
79 true,
80 );
81
82 let mut menubar = Menu::new();
83 menubar.add_dropdown(file_menu, "Application", true);
84
85 let mut builder = WindowBuilder::new(app.clone());
86 builder.set_handler(Box::<QuitState>::default());
87 builder.set_title("Quit example");
88 builder.set_menu(menubar);
89
90 let window = builder.build().unwrap();
91 window.show();
92
93 app.run(None);
94}
136fn main() {
137 tracing_subscriber::fmt().init();
138 let mut file_menu = Menu::new();
139 file_menu.add_item(
140 0x100,
141 "E&xit",
142 Some(&HotKey::new(SysMods::Cmd, "q")),
143 None,
144 true,
145 );
146 file_menu.add_item(
147 0x101,
148 "O&pen",
149 Some(&HotKey::new(SysMods::Cmd, "o")),
150 None,
151 true,
152 );
153 file_menu.add_item(
154 0x102,
155 "S&ave",
156 Some(&HotKey::new(SysMods::Cmd, "s")),
157 None,
158 true,
159 );
160 let mut menubar = Menu::new();
161 menubar.add_dropdown(Menu::new(), "Application", true);
162 menubar.add_dropdown(file_menu, "&File", true);
163
164 let app = Application::new().unwrap();
165 let mut builder = WindowBuilder::new(app.clone());
166 builder.set_handler(Box::<HelloState>::default());
167 builder.set_title("Hello example");
168 builder.set_menu(menubar);
169
170 let window = builder.build().unwrap();
171 window.show();
172
173 app.run(None);
174}
Sourcepub fn quit(&self)
pub fn quit(&self)
Quit the Application
.
This will cause run
to return control back to the calling function.
Examples found in repository?
More examples
119 fn command(&mut self, id: u32) {
120 match id {
121 0x100 => {
122 self.handle.close();
123 Application::global().quit()
124 }
125 _ => println!("unexpected id {id}"),
126 }
127 }
128
129 fn key_down(&mut self, event: KeyEvent) -> bool {
130 if event.key == Key::Character("c".to_string()) {
131 // custom hotkey for pressing "c"
132 println!("user pressed c! wow! setting selection to 0");
133
134 // update internal selection state
135 self.document.borrow_mut().selection = Selection::caret(0);
136
137 // notify the OS that we've updated the selection
138 self.handle
139 .update_text_field(self.text_input_token.unwrap(), Event::SelectionChanged);
140
141 // repaint window
142 self.handle.request_anim_frame();
143
144 // return true prevents the keypress event from being handled as text input
145 return true;
146 }
147 false
148 }
149
150 fn acquire_input_lock(
151 &mut self,
152 _token: TextFieldToken,
153 _mutable: bool,
154 ) -> Box<dyn InputHandler> {
155 Box::new(AppInputHandler {
156 state: self.document.clone(),
157 window_size: self.size,
158 window_handle: self.handle.clone(),
159 })
160 }
161
162 fn release_input_lock(&mut self, _token: TextFieldToken) {
163 // no action required; this example is simple enough that this
164 // state is not actually shared.
165 }
166
167 fn size(&mut self, size: Size) {
168 self.size = size;
169 }
170
171 fn request_close(&mut self) {
172 self.handle.close();
173 }
174
175 fn destroy(&mut self) {
176 Application::global().quit()
177 }
47 fn command(&mut self, id: u32) {
48 match id {
49 0x100 => {
50 self.handle.close();
51 Application::global().quit()
52 }
53 0x101 => {
54 let options = FileDialogOptions::new().show_hidden().allowed_types(vec![
55 FileSpec::new("Rust Files", &["rs", "toml"]),
56 FileSpec::TEXT,
57 FileSpec::JPG,
58 ]);
59 self.handle.open_file(options);
60 }
61 0x102 => {
62 let options = FileDialogOptions::new().show_hidden().allowed_types(vec![
63 FileSpec::new("Rust Files", &["rs", "toml"]),
64 FileSpec::TEXT,
65 FileSpec::JPG,
66 ]);
67 self.handle.save_as(options);
68 }
69 _ => println!("unexpected id {id}"),
70 }
71 }
72
73 fn open_file(&mut self, _token: FileDialogToken, file_info: Option<FileInfo>) {
74 println!("open file result: {file_info:?}");
75 }
76
77 fn save_as(&mut self, _token: FileDialogToken, file: Option<FileInfo>) {
78 println!("save file result: {file:?}");
79 }
80
81 fn key_down(&mut self, event: KeyEvent) -> bool {
82 println!("keydown: {event:?}");
83 false
84 }
85
86 fn key_up(&mut self, event: KeyEvent) {
87 println!("keyup: {event:?}");
88 }
89
90 fn wheel(&mut self, event: &MouseEvent) {
91 println!("mouse_wheel {event:?}");
92 }
93
94 fn mouse_move(&mut self, event: &MouseEvent) {
95 self.handle.set_cursor(&Cursor::Arrow);
96 println!("mouse_move {event:?}");
97 }
98
99 fn mouse_down(&mut self, event: &MouseEvent) {
100 println!("mouse_down {event:?}");
101 }
102
103 fn mouse_up(&mut self, event: &MouseEvent) {
104 println!("mouse_up {event:?}");
105 }
106
107 fn timer(&mut self, id: TimerToken) {
108 println!("timer fired: {id:?}");
109 }
110
111 fn size(&mut self, size: Size) {
112 self.size = size;
113 }
114
115 fn got_focus(&mut self) {
116 println!("Got focus");
117 }
118
119 fn lost_focus(&mut self) {
120 println!("Lost focus");
121 }
122
123 fn request_close(&mut self) {
124 self.handle.close();
125 }
126
127 fn destroy(&mut self) {
128 Application::global().quit()
129 }
Sourcepub fn get_locale() -> String
pub fn get_locale() -> String
Returns the current locale string.
This should a Unicode language identifier.
Trait Implementations§
Source§impl Clone for Application
impl Clone for Application
Source§fn clone(&self) -> Application
fn clone(&self) -> Application
1.0.0 · Source§const fn clone_from(&mut self, source: &Self)
const fn clone_from(&mut self, source: &Self)
source
. Read more