From b9bf30e6f192bd8b8b98ca206223c8f511b601fa Mon Sep 17 00:00:00 2001 From: Ryan McGrath Date: Thu, 6 Jan 2022 23:25:36 -0800 Subject: [PATCH] Fix examples, fix webview to work with new ObjcAccess trait, rename Image icon method for toolbars --- examples/calculator/content_view.rs | 2 +- examples/calculator/main.rs | 2 +- examples/frame_layout.rs | 30 ++++++++++++---------- examples/todos_list/add/view.rs | 4 ++- examples/todos_list/preferences/toolbar.rs | 4 +-- examples/todos_list/todos/list/mod.rs | 1 + src/image/image.rs | 4 +-- src/webview/mod.rs | 9 ++++--- 8 files changed, 33 insertions(+), 23 deletions(-) diff --git a/examples/calculator/content_view.rs b/examples/calculator/content_view.rs index bed52b8..cda706d 100644 --- a/examples/calculator/content_view.rs +++ b/examples/calculator/content_view.rs @@ -17,7 +17,7 @@ pub fn button(text: &str, msg: Msg) -> Button { button.set_bezel_style(BezelStyle::SmallSquare); button.set_focus_ring_type(FocusRingType::None); button.set_action(move || dispatch(msg.clone())); - button.set_key_equivalent(&text.to_lowercase()); + button.set_key_equivalent(&*text.to_lowercase()); let font = Font::system(22.); button.set_font(&font); diff --git a/examples/calculator/main.rs b/examples/calculator/main.rs index eeec9fd..d36ef02 100644 --- a/examples/calculator/main.rs +++ b/examples/calculator/main.rs @@ -37,7 +37,7 @@ impl AppDelegate for CalculatorApp { // 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(); + //self.start_monitoring(); self.window.set_title("Calculator"); self.window.set_background_color(Color::rgb(49,49,49)); diff --git a/examples/frame_layout.rs b/examples/frame_layout.rs index c486ae3..ca84591 100644 --- a/examples/frame_layout.rs +++ b/examples/frame_layout.rs @@ -10,6 +10,12 @@ use cacao::appkit::{App, AppDelegate}; use cacao::appkit::menu::{Menu, MenuItem}; use cacao::appkit::window::{Window, WindowConfig, WindowDelegate}; +const CORNER_RADIUS: f64 = 16.; +const SPACING: f64 = 10.; +const TOP: f64 = 40.; +const WIDTH: f64 = 100.; +const HEIGHT: f64 = 100.; + struct BasicApp { window: Window } @@ -46,6 +52,7 @@ impl AppDelegate for BasicApp { App::activate(); self.window.show(); + self.window.delegate.as_ref().unwrap().layout(); } fn should_terminate_after_last_window_closed(&self) -> bool { @@ -61,19 +68,8 @@ struct AppWindow { green: View } -const CORNER_RADIUS: f64 = 16.; -const SPACING: f64 = 10.; -const TOP: f64 = 40.; -const WIDTH: f64 = 100.; -const HEIGHT: f64 = 100.; - -impl WindowDelegate for AppWindow { - const NAME: &'static str = "WindowDelegate"; - - fn did_load(&mut self, window: Window) { - window.set_title("Frame Layout Example"); - window.set_minimum_content_size(300., 300.); - +impl AppWindow { + pub fn layout(&self) { self.blue.set_background_color(Color::SystemBlue); self.blue.set_frame(Rect { top: TOP, @@ -103,7 +99,15 @@ impl WindowDelegate for AppWindow { }); self.green.layer.set_corner_radius(CORNER_RADIUS); self.content.add_subview(&self.green); + } +} +impl WindowDelegate for AppWindow { + const NAME: &'static str = "WindowDelegate"; + + fn did_load(&mut self, window: Window) { + window.set_title("Frame Layout Example"); + window.set_minimum_content_size(300., 300.); window.set_content_view(&self.content); } } diff --git a/examples/todos_list/add/view.rs b/examples/todos_list/add/view.rs index f6ef23b..d4712cf 100644 --- a/examples/todos_list/add/view.rs +++ b/examples/todos_list/add/view.rs @@ -49,6 +49,7 @@ impl ViewDelegate for AddNewTodoContentView { let input = TextField::new(); let mut button = Button::new("Add"); + button.set_key_equivalent("\r"); button.set_action(|| dispatch_ui(Message::ProcessNewTodo)); view.add_subview(&instructions); @@ -65,7 +66,8 @@ impl ViewDelegate for AddNewTodoContentView { input.trailing.constraint_equal_to(&view.trailing).offset(-16.), button.top.constraint_equal_to(&input.bottom).offset(8.), - button.trailing.constraint_equal_to(&view.trailing).offset(-16.) + button.trailing.constraint_equal_to(&view.trailing).offset(-16.), + button.bottom.constraint_equal_to(&view.bottom).offset(-16.) ]); self.view = Some(view); diff --git a/examples/todos_list/preferences/toolbar.rs b/examples/todos_list/preferences/toolbar.rs index 895b4cb..201998a 100644 --- a/examples/todos_list/preferences/toolbar.rs +++ b/examples/todos_list/preferences/toolbar.rs @@ -15,7 +15,7 @@ impl Default for PreferencesToolbar { let mut item = ToolbarItem::new("general"); item.set_title("General"); - let icon = Image::system_icon(MacSystemIcon::PreferencesGeneral, "General"); + let icon = Image::toolbar_icon(MacSystemIcon::PreferencesGeneral, "General"); item.set_image(icon); item.set_action(|| { @@ -27,7 +27,7 @@ impl Default for PreferencesToolbar { let mut item = ToolbarItem::new("advanced"); item.set_title("Advanced"); - let icon = Image::system_icon(MacSystemIcon::PreferencesAdvanced, "Advanced"); + let icon = Image::toolbar_icon(MacSystemIcon::PreferencesAdvanced, "Advanced"); item.set_image(icon); item.set_action(|| { diff --git a/examples/todos_list/todos/list/mod.rs b/examples/todos_list/todos/list/mod.rs index e54925b..5984878 100644 --- a/examples/todos_list/todos/list/mod.rs +++ b/examples/todos_list/todos/list/mod.rs @@ -64,6 +64,7 @@ impl ListViewDelegate for TodosListView { fn did_load(&mut self, view: ListView) { view.register(TODO_ROW, TodoViewRow::default); view.set_uses_alternating_backgrounds(true); + view.set_row_height(64.); self.view = Some(view); } diff --git a/src/image/image.rs b/src/image/image.rs index 599ec4e..455e437 100644 --- a/src/image/image.rs +++ b/src/image/image.rs @@ -172,12 +172,12 @@ impl Image { /// /// A system symbol will swap an SFSymbol in for macOS 11.0+, but return the correct /// MacSystemIcon image type for versions prior to that. This is mostly helpful in situations - /// like Preferences windows, where you want to have the correct modern styling for newer OS + /// like Preferences windows toolbars, where you want to have the correct modern styling for newer OS /// versions. /// /// However, if you need the correct "folder" icon for instance, you probably want `system_icon`. #[cfg(target_os = "macos")] - pub fn system_symbol(icon: MacSystemIcon, accessibility_description: &str) -> Self { + pub fn toolbar_icon(icon: MacSystemIcon, accessibility_description: &str) -> Self { Image(unsafe { ShareId::from_ptr(match os::is_minimum_version(11) { true => { diff --git a/src/webview/mod.rs b/src/webview/mod.rs index ab27ad1..cc9c301 100644 --- a/src/webview/mod.rs +++ b/src/webview/mod.rs @@ -23,6 +23,7 @@ use crate::foundation::{id, nil, YES, NO, NSString}; use crate::geometry::Rect; use crate::layout::Layout; use crate::layer::Layer; +use crate::objc_access::ObjcAccess; use crate::utils::properties::ObjcProperty; #[cfg(feature = "autolayout")] @@ -334,15 +335,17 @@ impl WebView { } } -impl Layout for WebView { - fn with_backing_node(&self, handler: F) { +impl ObjcAccess for WebView { + fn with_backing_obj_mut(&self, handler: F) { self.objc.with_mut(handler); } - fn get_from_backing_node R, R>(&self, handler: F) -> R { + fn get_from_backing_obj R, R>(&self, handler: F) -> R { self.objc.get(handler) } +} +impl Layout for WebView { /// Currently, this is a noop. Theoretically there is reason to support this, but in practice /// I've never seen it needed... but am open to discussion. fn add_subview(&self, _: &V) {}