2021-02-13 12:57:06 +11:00
|
|
|
//! This example implements a basic macOS Calculator clone. It showcases:
|
|
|
|
//!
|
|
|
|
//! - A single-window app
|
|
|
|
//! - Button handling
|
|
|
|
//! - Autolayout
|
|
|
|
//! - Message dispatching
|
|
|
|
//! - Global key/event handling
|
|
|
|
//!
|
|
|
|
//! It does not attempt to be a good calculator, and does not implement the
|
|
|
|
//! extended Calculator view.
|
|
|
|
|
|
|
|
use std::sync::RwLock;
|
|
|
|
|
2022-07-16 00:14:02 +10:00
|
|
|
use cacao::appkit::window::{TitleVisibility, Window, WindowConfig};
|
2021-08-08 15:31:48 +10:00
|
|
|
use cacao::appkit::{App, AppDelegate};
|
|
|
|
use cacao::appkit::{Event, EventMask, EventMonitor};
|
2021-02-13 12:57:06 +11:00
|
|
|
use cacao::color::Color;
|
|
|
|
use cacao::notification_center::Dispatcher;
|
2021-03-05 12:24:39 +11:00
|
|
|
use cacao::view::View;
|
2021-02-13 12:57:06 +11:00
|
|
|
|
|
|
|
mod button_row;
|
|
|
|
mod calculator;
|
|
|
|
|
|
|
|
mod content_view;
|
|
|
|
use content_view::CalculatorView;
|
|
|
|
|
|
|
|
struct CalculatorApp {
|
|
|
|
window: Window,
|
|
|
|
content: View<CalculatorView>,
|
2022-08-19 07:45:19 +10:00
|
|
|
key_monitor: RwLock<Option<EventMonitor>>
|
2021-02-13 12:57:06 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
impl AppDelegate for CalculatorApp {
|
|
|
|
fn did_finish_launching(&self) {
|
|
|
|
App::activate();
|
|
|
|
|
|
|
|
// Event Monitors need to be started after the App has been activated.
|
2022-07-11 01:15:29 +10:00
|
|
|
// We use an RwLock here, but it's possible this entire method can be
|
2021-02-13 12:57:06 +11:00
|
|
|
// &mut self and you wouldn't need these kinds of shenanigans.
|
2022-01-07 18:25:36 +11:00
|
|
|
//self.start_monitoring();
|
2021-02-13 12:57:06 +11:00
|
|
|
|
|
|
|
self.window.set_title("Calculator");
|
2022-07-16 00:14:02 +10:00
|
|
|
self.window.set_background_color(Color::rgb(49, 49, 49));
|
2021-02-13 12:57:06 +11:00
|
|
|
self.window.set_title_visibility(TitleVisibility::Hidden);
|
|
|
|
self.window.set_titlebar_appears_transparent(true);
|
|
|
|
self.window.set_movable_by_background(true);
|
|
|
|
self.window.set_autosave_name("CacaoCalculatorExampleWindow");
|
|
|
|
self.window.set_content_view(&self.content);
|
|
|
|
self.window.show();
|
|
|
|
}
|
2021-08-08 15:31:48 +10:00
|
|
|
|
2021-03-01 22:32:14 +11:00
|
|
|
fn should_terminate_after_last_window_closed(&self) -> bool {
|
|
|
|
true
|
|
|
|
}
|
2021-02-13 12:57:06 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Dispatcher for CalculatorApp {
|
|
|
|
type Message = String;
|
|
|
|
|
|
|
|
fn on_ui_message(&self, message: Self::Message) {
|
|
|
|
if let Some(delegate) = &self.content.delegate {
|
|
|
|
delegate.render_update(message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CalculatorApp {
|
|
|
|
/// Monitor for key presses, and dispatch if they match an action
|
|
|
|
/// we're after.
|
|
|
|
pub fn start_monitoring(&self) {
|
|
|
|
let mut lock = self.key_monitor.write().unwrap();
|
|
|
|
*lock = Some(Event::local_monitor(EventMask::KeyDown, |evt| {
|
|
|
|
let characters = evt.characters();
|
|
|
|
println!("{}", characters);
|
|
|
|
|
2021-03-05 12:24:39 +11:00
|
|
|
//use calculator::{dispatch, Msg};
|
|
|
|
/*match characters.as_ref() {
|
2021-02-13 12:57:06 +11:00
|
|
|
"0" => dispatch(Msg::Push(0)),
|
|
|
|
"1" => dispatch(Msg::Push(1)),
|
|
|
|
"2" => dispatch(Msg::Push(2)),
|
|
|
|
"3" => dispatch(Msg::Push(3)),
|
|
|
|
"4" => dispatch(Msg::Push(4)),
|
|
|
|
"5" => dispatch(Msg::Push(5)),
|
|
|
|
"6" => dispatch(Msg::Push(6)),
|
|
|
|
"7" => dispatch(Msg::Push(7)),
|
|
|
|
"8" => dispatch(Msg::Push(8)),
|
|
|
|
"9" => dispatch(Msg::Push(9)),
|
|
|
|
"+" => dispatch(Msg::Add),
|
|
|
|
"-" => dispatch(Msg::Subtract),
|
|
|
|
"*" => dispatch(Msg::Multiply),
|
|
|
|
"/" => dispatch(Msg::Divide),
|
|
|
|
"=" => dispatch(Msg::Equals),
|
|
|
|
"%" => dispatch(Msg::Mod),
|
|
|
|
"c" => dispatch(Msg::Clear),
|
|
|
|
"." => dispatch(Msg::Decimal),
|
|
|
|
_ => {}
|
2021-03-05 12:24:39 +11:00
|
|
|
}*/
|
2021-02-13 12:57:06 +11:00
|
|
|
|
|
|
|
None
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let mut config = WindowConfig::default();
|
|
|
|
config.set_initial_dimensions(100., 100., 240., 300.);
|
|
|
|
|
2022-08-19 07:45:19 +10:00
|
|
|
App::new("com.example.calculator", CalculatorApp {
|
|
|
|
window: Window::new(config),
|
|
|
|
content: View::with(CalculatorView::new()),
|
|
|
|
key_monitor: RwLock::new(None)
|
|
|
|
})
|
2022-07-16 00:14:02 +10:00
|
|
|
.run();
|
2021-02-13 12:57:06 +11:00
|
|
|
}
|