Fix examples, fix webview to work with new ObjcAccess trait, rename Image icon method for toolbars

This commit is contained in:
Ryan McGrath 2022-01-06 23:25:36 -08:00
parent 4ecfbd0928
commit b9bf30e6f1
No known key found for this signature in database
GPG key ID: DA6CBD9233593DEA
8 changed files with 33 additions and 23 deletions

View file

@ -17,7 +17,7 @@ pub fn button(text: &str, msg: Msg) -> Button {
button.set_bezel_style(BezelStyle::SmallSquare); button.set_bezel_style(BezelStyle::SmallSquare);
button.set_focus_ring_type(FocusRingType::None); button.set_focus_ring_type(FocusRingType::None);
button.set_action(move || dispatch(msg.clone())); 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.); let font = Font::system(22.);
button.set_font(&font); button.set_font(&font);

View file

@ -37,7 +37,7 @@ impl AppDelegate for CalculatorApp {
// Event Monitors need to be started after the App has been activated. // 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 // 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. // &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_title("Calculator");
self.window.set_background_color(Color::rgb(49,49,49)); self.window.set_background_color(Color::rgb(49,49,49));

View file

@ -10,6 +10,12 @@ use cacao::appkit::{App, AppDelegate};
use cacao::appkit::menu::{Menu, MenuItem}; use cacao::appkit::menu::{Menu, MenuItem};
use cacao::appkit::window::{Window, WindowConfig, WindowDelegate}; 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 { struct BasicApp {
window: Window<AppWindow> window: Window<AppWindow>
} }
@ -46,6 +52,7 @@ impl AppDelegate for BasicApp {
App::activate(); App::activate();
self.window.show(); self.window.show();
self.window.delegate.as_ref().unwrap().layout();
} }
fn should_terminate_after_last_window_closed(&self) -> bool { fn should_terminate_after_last_window_closed(&self) -> bool {
@ -61,19 +68,8 @@ struct AppWindow {
green: View green: View
} }
const CORNER_RADIUS: f64 = 16.; impl AppWindow {
const SPACING: f64 = 10.; pub fn layout(&self) {
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.);
self.blue.set_background_color(Color::SystemBlue); self.blue.set_background_color(Color::SystemBlue);
self.blue.set_frame(Rect { self.blue.set_frame(Rect {
top: TOP, top: TOP,
@ -103,7 +99,15 @@ impl WindowDelegate for AppWindow {
}); });
self.green.layer.set_corner_radius(CORNER_RADIUS); self.green.layer.set_corner_radius(CORNER_RADIUS);
self.content.add_subview(&self.green); 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); window.set_content_view(&self.content);
} }
} }

View file

@ -49,6 +49,7 @@ impl ViewDelegate for AddNewTodoContentView {
let input = TextField::new(); let input = TextField::new();
let mut button = Button::new("Add"); let mut button = Button::new("Add");
button.set_key_equivalent("\r");
button.set_action(|| dispatch_ui(Message::ProcessNewTodo)); button.set_action(|| dispatch_ui(Message::ProcessNewTodo));
view.add_subview(&instructions); view.add_subview(&instructions);
@ -65,7 +66,8 @@ impl ViewDelegate for AddNewTodoContentView {
input.trailing.constraint_equal_to(&view.trailing).offset(-16.), input.trailing.constraint_equal_to(&view.trailing).offset(-16.),
button.top.constraint_equal_to(&input.bottom).offset(8.), 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); self.view = Some(view);

View file

@ -15,7 +15,7 @@ impl Default for PreferencesToolbar {
let mut item = ToolbarItem::new("general"); let mut item = ToolbarItem::new("general");
item.set_title("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_image(icon);
item.set_action(|| { item.set_action(|| {
@ -27,7 +27,7 @@ impl Default for PreferencesToolbar {
let mut item = ToolbarItem::new("advanced"); let mut item = ToolbarItem::new("advanced");
item.set_title("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_image(icon);
item.set_action(|| { item.set_action(|| {

View file

@ -64,6 +64,7 @@ impl ListViewDelegate for TodosListView {
fn did_load(&mut self, view: ListView) { fn did_load(&mut self, view: ListView) {
view.register(TODO_ROW, TodoViewRow::default); view.register(TODO_ROW, TodoViewRow::default);
view.set_uses_alternating_backgrounds(true); view.set_uses_alternating_backgrounds(true);
view.set_row_height(64.);
self.view = Some(view); self.view = Some(view);
} }

View file

@ -172,12 +172,12 @@ impl Image {
/// ///
/// A system symbol will swap an SFSymbol in for macOS 11.0+, but return the correct /// 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 /// 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. /// versions.
/// ///
/// However, if you need the correct "folder" icon for instance, you probably want `system_icon`. /// However, if you need the correct "folder" icon for instance, you probably want `system_icon`.
#[cfg(target_os = "macos")] #[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 { Image(unsafe {
ShareId::from_ptr(match os::is_minimum_version(11) { ShareId::from_ptr(match os::is_minimum_version(11) {
true => { true => {

View file

@ -23,6 +23,7 @@ use crate::foundation::{id, nil, YES, NO, NSString};
use crate::geometry::Rect; use crate::geometry::Rect;
use crate::layout::Layout; use crate::layout::Layout;
use crate::layer::Layer; use crate::layer::Layer;
use crate::objc_access::ObjcAccess;
use crate::utils::properties::ObjcProperty; use crate::utils::properties::ObjcProperty;
#[cfg(feature = "autolayout")] #[cfg(feature = "autolayout")]
@ -334,15 +335,17 @@ impl<T> WebView<T> {
} }
} }
impl<T> Layout for WebView<T> { impl<T> ObjcAccess for WebView<T> {
fn with_backing_node<F: Fn(id)>(&self, handler: F) { fn with_backing_obj_mut<F: Fn(id)>(&self, handler: F) {
self.objc.with_mut(handler); self.objc.with_mut(handler);
} }
fn get_from_backing_node<F: Fn(&Object) -> R, R>(&self, handler: F) -> R { fn get_from_backing_obj<F: Fn(&Object) -> R, R>(&self, handler: F) -> R {
self.objc.get(handler) self.objc.get(handler)
} }
}
impl<T> Layout for WebView<T> {
/// Currently, this is a noop. Theoretically there is reason to support this, but in practice /// 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. /// I've never seen it needed... but am open to discussion.
fn add_subview<V: Layout>(&self, _: &V) {} fn add_subview<V: Layout>(&self, _: &V) {}