cacao/examples/calculator/main.rs

113 lines
3.6 KiB
Rust
Raw Normal View History

Begin reworking many internals to push for v0.1. - Beginning to transition View types to use Rc/RefCell internally, which should provide better guarantees about ownership on the Rust side. This is also important for certain Objective-C side scenarios where we may need to set an ivar after creation, which requires some level of mutability. This may also possibly help bring down the unsafe usage, which would be cool. - Rewrote the Color module; this now handles system colors better, and provides support for dynamic color creation. Supporting combinations of dark/light/contrast is now possible with handler passed in via `Color::dynamic`. This still has work to do in terms of some accessor methods and such, but it works well for now. The `to_platform...` method should be removed before v0.1. - Added a new feature for enabling fallback color usage on older macOS versions that don't support system colors. This may honestly never be used, but it cost nothing to implement. - Fixed a bug in the Autolayout wrapper where dereferencing could cause constraints to crash at runtime. - Support setting text color on labels. - Support setting text color on buttons, albeit very hacky right now. This needs to be extracted and/or cleaned up, but getting it sketched out was important for this commit. - Support setting a key equivalent on buttons. - Creating a local event monitor is now possible. - Examples updated; Calculator clone example added. The only API breaking change in this commit from earlier commits should be `color::rgb` needing to be `color::Color` followed by a `Color::rgb(...)` call.
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;
use cacao::macos::{App, AppDelegate};
use cacao::macos::window::{Window, WindowConfig, TitleVisibility};
use cacao::macos::{Event, EventMask, EventMonitor};
use cacao::color::Color;
use cacao::notification_center::Dispatcher;
use cacao::view::View;
Begin reworking many internals to push for v0.1. - Beginning to transition View types to use Rc/RefCell internally, which should provide better guarantees about ownership on the Rust side. This is also important for certain Objective-C side scenarios where we may need to set an ivar after creation, which requires some level of mutability. This may also possibly help bring down the unsafe usage, which would be cool. - Rewrote the Color module; this now handles system colors better, and provides support for dynamic color creation. Supporting combinations of dark/light/contrast is now possible with handler passed in via `Color::dynamic`. This still has work to do in terms of some accessor methods and such, but it works well for now. The `to_platform...` method should be removed before v0.1. - Added a new feature for enabling fallback color usage on older macOS versions that don't support system colors. This may honestly never be used, but it cost nothing to implement. - Fixed a bug in the Autolayout wrapper where dereferencing could cause constraints to crash at runtime. - Support setting text color on labels. - Support setting text color on buttons, albeit very hacky right now. This needs to be extracted and/or cleaned up, but getting it sketched out was important for this commit. - Support setting a key equivalent on buttons. - Creating a local event monitor is now possible. - Examples updated; Calculator clone example added. The only API breaking change in this commit from earlier commits should be `color::rgb` needing to be `color::Color` followed by a `Color::rgb(...)` call.
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>,
key_monitor: RwLock<Option<EventMonitor>>
}
impl AppDelegate for CalculatorApp {
fn did_finish_launching(&self) {
App::activate();
// Event Monitors need to be started after the App has been activated.
// We use an RwLock here, but it's possible this entire method can be
// &mut self and you wouldn't need these kinds of shenanigans.
self.start_monitoring();
Begin reworking many internals to push for v0.1. - Beginning to transition View types to use Rc/RefCell internally, which should provide better guarantees about ownership on the Rust side. This is also important for certain Objective-C side scenarios where we may need to set an ivar after creation, which requires some level of mutability. This may also possibly help bring down the unsafe usage, which would be cool. - Rewrote the Color module; this now handles system colors better, and provides support for dynamic color creation. Supporting combinations of dark/light/contrast is now possible with handler passed in via `Color::dynamic`. This still has work to do in terms of some accessor methods and such, but it works well for now. The `to_platform...` method should be removed before v0.1. - Added a new feature for enabling fallback color usage on older macOS versions that don't support system colors. This may honestly never be used, but it cost nothing to implement. - Fixed a bug in the Autolayout wrapper where dereferencing could cause constraints to crash at runtime. - Support setting text color on labels. - Support setting text color on buttons, albeit very hacky right now. This needs to be extracted and/or cleaned up, but getting it sketched out was important for this commit. - Support setting a key equivalent on buttons. - Creating a local event monitor is now possible. - Examples updated; Calculator clone example added. The only API breaking change in this commit from earlier commits should be `color::rgb` needing to be `color::Color` followed by a `Color::rgb(...)` call.
2021-02-13 12:57:06 +11:00
self.window.set_title("Calculator");
self.window.set_background_color(Color::rgb(49,49,49));
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();
}
fn should_terminate_after_last_window_closed(&self) -> bool {
true
}
Begin reworking many internals to push for v0.1. - Beginning to transition View types to use Rc/RefCell internally, which should provide better guarantees about ownership on the Rust side. This is also important for certain Objective-C side scenarios where we may need to set an ivar after creation, which requires some level of mutability. This may also possibly help bring down the unsafe usage, which would be cool. - Rewrote the Color module; this now handles system colors better, and provides support for dynamic color creation. Supporting combinations of dark/light/contrast is now possible with handler passed in via `Color::dynamic`. This still has work to do in terms of some accessor methods and such, but it works well for now. The `to_platform...` method should be removed before v0.1. - Added a new feature for enabling fallback color usage on older macOS versions that don't support system colors. This may honestly never be used, but it cost nothing to implement. - Fixed a bug in the Autolayout wrapper where dereferencing could cause constraints to crash at runtime. - Support setting text color on labels. - Support setting text color on buttons, albeit very hacky right now. This needs to be extracted and/or cleaned up, but getting it sketched out was important for this commit. - Support setting a key equivalent on buttons. - Creating a local event monitor is now possible. - Examples updated; Calculator clone example added. The only API breaking change in this commit from earlier commits should be `color::rgb` needing to be `color::Color` followed by a `Color::rgb(...)` call.
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);
//use calculator::{dispatch, Msg};
/*match characters.as_ref() {
Begin reworking many internals to push for v0.1. - Beginning to transition View types to use Rc/RefCell internally, which should provide better guarantees about ownership on the Rust side. This is also important for certain Objective-C side scenarios where we may need to set an ivar after creation, which requires some level of mutability. This may also possibly help bring down the unsafe usage, which would be cool. - Rewrote the Color module; this now handles system colors better, and provides support for dynamic color creation. Supporting combinations of dark/light/contrast is now possible with handler passed in via `Color::dynamic`. This still has work to do in terms of some accessor methods and such, but it works well for now. The `to_platform...` method should be removed before v0.1. - Added a new feature for enabling fallback color usage on older macOS versions that don't support system colors. This may honestly never be used, but it cost nothing to implement. - Fixed a bug in the Autolayout wrapper where dereferencing could cause constraints to crash at runtime. - Support setting text color on labels. - Support setting text color on buttons, albeit very hacky right now. This needs to be extracted and/or cleaned up, but getting it sketched out was important for this commit. - Support setting a key equivalent on buttons. - Creating a local event monitor is now possible. - Examples updated; Calculator clone example added. The only API breaking change in this commit from earlier commits should be `color::rgb` needing to be `color::Color` followed by a `Color::rgb(...)` call.
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),
_ => {}
}*/
Begin reworking many internals to push for v0.1. - Beginning to transition View types to use Rc/RefCell internally, which should provide better guarantees about ownership on the Rust side. This is also important for certain Objective-C side scenarios where we may need to set an ivar after creation, which requires some level of mutability. This may also possibly help bring down the unsafe usage, which would be cool. - Rewrote the Color module; this now handles system colors better, and provides support for dynamic color creation. Supporting combinations of dark/light/contrast is now possible with handler passed in via `Color::dynamic`. This still has work to do in terms of some accessor methods and such, but it works well for now. The `to_platform...` method should be removed before v0.1. - Added a new feature for enabling fallback color usage on older macOS versions that don't support system colors. This may honestly never be used, but it cost nothing to implement. - Fixed a bug in the Autolayout wrapper where dereferencing could cause constraints to crash at runtime. - Support setting text color on labels. - Support setting text color on buttons, albeit very hacky right now. This needs to be extracted and/or cleaned up, but getting it sketched out was important for this commit. - Support setting a key equivalent on buttons. - Creating a local event monitor is now possible. - Examples updated; Calculator clone example added. The only API breaking change in this commit from earlier commits should be `color::rgb` needing to be `color::Color` followed by a `Color::rgb(...)` call.
2021-02-13 12:57:06 +11:00
None
}));
}
}
fn main() {
let mut config = WindowConfig::default();
config.set_initial_dimensions(100., 100., 240., 300.);
App::new("com.example.calculator", CalculatorApp {
window: Window::new(config),
content: View::with(CalculatorView::new()),
key_monitor: RwLock::new(None)
}).run();
}