Working on fixing up docs, general cleanup

This commit is contained in:
Ryan McGrath 2020-03-26 20:19:52 -07:00
parent ebec770581
commit b573fba459
No known key found for this signature in database
GPG key ID: 811674B62B666830
4 changed files with 51 additions and 23 deletions

View file

@ -1,5 +1,6 @@
//! A wrapper for `NSApplicationDelegate` on macOS. Handles looping back events and providing a very janky //! A wrapper for `NSApplicationDelegate` on macOS. Handles looping back events and providing a very janky
//! messaging architecture. //! messaging architecture.
//!
use objc_id::Id; use objc_id::Id;
use objc::runtime::Object; use objc::runtime::Object;

View file

@ -1,9 +1,10 @@
//! This module includes wrappers for `CKShare` and `CKShareMetaData`. //! This module includes wrappers for `CKShare` and `CKShareMetaData`.
use cocoa::base::id;
use objc::runtime::Object; use objc::runtime::Object;
use objc_id::ShareId; use objc_id::ShareId;
use crate::foundation::id;
/// A wrapper for `CKShareMetaData`, which describes details about a `CKShare`. You might use this /// A wrapper for `CKShareMetaData`, which describes details about a `CKShare`. You might use this
/// to, say, handle accepting an invite for a share. /// to, say, handle accepting an invite for a share.
#[derive(Clone, Debug)] #[derive(Clone, Debug)]

View file

@ -8,11 +8,13 @@
use std::rc::Rc; use std::rc::Rc;
use std::cell::RefCell; use std::cell::RefCell;
use core_graphics::geometry::CGRect;
use objc_id::ShareId; use objc_id::ShareId;
use objc::runtime::Object; use objc::runtime::Object;
use objc::{class, msg_send, sel, sel_impl}; use objc::{class, msg_send, sel, sel_impl};
use crate::foundation::{id, YES, NO, CGRect, NSString}; use crate::foundation::{id, YES, NO, NSString};
use crate::geometry::Rect; use crate::geometry::Rect;
use crate::layout::{Layout, LayoutAnchorX, LayoutAnchorY, LayoutAnchorDimension}; use crate::layout::{Layout, LayoutAnchorX, LayoutAnchorY, LayoutAnchorDimension};

View file

@ -1,5 +1,10 @@
//! Implements an `NSWindow` wrapper for MacOS, backed by Cocoa and associated widgets. This also handles looping back //! Implements an `NSWindow` wrapper for MacOS, backed by Cocoa and associated widgets. This also handles looping back
//! lifecycle events, such as window resizing or close events. //! lifecycle events, such as window resizing or close events. It currently implements a good chunk
//! of the API, however it should be noted that in places where things are outright deprecated,
//! this framework will opt to not bother providing access to them.
//!
//! If you require functionality like that, you're free to use the `objc` field on a `Window` to
//! instrument it with the Objective-C runtime on your own.
use std::unreachable; use std::unreachable;
use std::rc::Rc; use std::rc::Rc;
@ -49,13 +54,18 @@ pub struct Window<T = ()> {
} }
impl Default for Window { impl Default for Window {
/// Returns a default `Window`, with a default `WindowConfig`.
fn default() -> Self { fn default() -> Self {
Window::new(WindowConfig::default()) Window::new(WindowConfig::default())
} }
} }
impl Window { impl Window {
/// Constructs a new Window. /// Constructs a new `Window`. You can use this instead of the `default()` method if you'd like
/// to customize the appearance of a `Window`.
///
/// Why the config? Well, certain properties of windows are really not meant to be altered
/// after we initialize the backing `NSWindow`.
pub fn new(config: WindowConfig) -> Window { pub fn new(config: WindowConfig) -> Window {
let objc = unsafe { let objc = unsafe {
let alloc: id = msg_send![register_window_class(), alloc]; let alloc: id = msg_send![register_window_class(), alloc];
@ -159,6 +169,25 @@ impl<T> Window<T> {
let _: () = msg_send![&*self.objc, setToolbar:&*toolbar.objc_controller.0]; let _: () = msg_send![&*self.objc, setToolbar:&*toolbar.objc_controller.0];
} }
} }
/// Toggles whether the toolbar is shown for this window. Has no effect if no toolbar exists on
/// this window.
pub fn toggle_toolbar_shown(&self) {
unsafe {
let _: () = msg_send![&*self.objc, toggleToolbarShown:nil];
}
}
/// Set whether the toolbar toggle button is shown. Has no effect if no toolbar exists on this
/// window.
pub fn set_shows_toolbar_button(&self, shows: bool) {
unsafe {
let _: () = msg_send![&*self.objc, setShowsToolbarButton:match shows {
true => YES,
false => NO
}];
}
}
/// Given a view, sets it as the content view for this window. /// Given a view, sets it as the content view for this window.
pub fn set_content_view<L: Layout + 'static>(&self, view: &L) { pub fn set_content_view<L: Layout + 'static>(&self, view: &L) {
@ -237,6 +266,7 @@ impl<T> Window<T> {
} }
} }
/// Returns whether this window is visible or not.
pub fn is_visible(&self) -> bool { pub fn is_visible(&self) -> bool {
unsafe { unsafe {
match msg_send![&*self.objc, isVisible] { match msg_send![&*self.objc, isVisible] {
@ -247,6 +277,7 @@ impl<T> Window<T> {
} }
} }
/// Returns whether this window is the key or not.
pub fn is_key(&self) -> bool { pub fn is_key(&self) -> bool {
unsafe { unsafe {
match msg_send![&*self.objc, isKeyWindow] { match msg_send![&*self.objc, isKeyWindow] {
@ -257,6 +288,7 @@ impl<T> Window<T> {
} }
} }
/// Returns whether this window can become the key window.
pub fn can_become_key(&self) -> bool { pub fn can_become_key(&self) -> bool {
unsafe { unsafe {
match msg_send![&*self.objc, canBecomeKeyWindow] { match msg_send![&*self.objc, canBecomeKeyWindow] {
@ -267,18 +299,22 @@ impl<T> Window<T> {
} }
} }
/// Make this window the key window.
pub fn make_key_window(&self) { pub fn make_key_window(&self) {
unsafe { unsafe {
let _: () = msg_send![&*self.objc, makeKeyWindow]; let _: () = msg_send![&*self.objc, makeKeyWindow];
} }
} }
/// Make the this window the key window and bring it to the front. Calling `show` does this for
/// you.
pub fn make_key_and_order_front(&self) { pub fn make_key_and_order_front(&self) {
unsafe { unsafe {
let _: () = msg_send![&*self.objc, makeKeyAndOrderFront:nil]; let _: () = msg_send![&*self.objc, makeKeyAndOrderFront:nil];
} }
} }
/// Returns if this is the main window or not.
pub fn is_main_window(&self) -> bool { pub fn is_main_window(&self) -> bool {
unsafe { unsafe {
match msg_send![&*self.objc, isMainWindow] { match msg_send![&*self.objc, isMainWindow] {
@ -289,6 +325,7 @@ impl<T> Window<T> {
} }
} }
/// Returns if this can become the main window.
pub fn can_become_main_window(&self) -> bool { pub fn can_become_main_window(&self) -> bool {
unsafe { unsafe {
match msg_send![&*self.objc, canBecomeMainWindow] { match msg_send![&*self.objc, canBecomeMainWindow] {
@ -299,12 +336,7 @@ impl<T> Window<T> {
} }
} }
pub fn toggle_toolbar_shown(&self) { /// Set whether this window should be excluded from the top-level "Windows" menu.
unsafe {
let _: () = msg_send![&*self.objc, toggleToolbarShown:nil];
}
}
pub fn set_excluded_from_windows_menu(&self, excluded: bool) { pub fn set_excluded_from_windows_menu(&self, excluded: bool) {
unsafe { unsafe {
let _: () = msg_send![&*self.objc, setExcludedFromWindowsMenu:match excluded { let _: () = msg_send![&*self.objc, setExcludedFromWindowsMenu:match excluded {
@ -313,16 +345,7 @@ impl<T> Window<T> {
}]; }];
} }
} }
pub fn set_shows_toolbar_button(&self, shows: bool) {
unsafe {
let _: () = msg_send![&*self.objc, setShowsToolbarButton:match shows {
true => YES,
false => NO
}];
}
}
/// Returns the backing scale (e.g, `1.0` for non retina, `2.0` for retina) used on this /// Returns the backing scale (e.g, `1.0` for non retina, `2.0` for retina) used on this
/// window. /// window.
/// ///
@ -334,12 +357,13 @@ impl<T> Window<T> {
scale as f64 scale as f64
} }
} }
} }
impl<T> Window<T> where T: WindowDelegate + 'static { impl<T> Window<T> where T: WindowDelegate + 'static {
/// Constructs a new Window. /// Constructs a new Window with a `config` and `delegate`. Using a `WindowDelegate` enables
/// you to respond to window lifecycle events - visibility, movement, and so on. It also
/// enables easier structure of your codebase, and in a way simulates traditional class based
/// architectures... just without the subclassing.
pub fn with(config: WindowConfig, delegate: T) -> Self { pub fn with(config: WindowConfig, delegate: T) -> Self {
let delegate = Rc::new(RefCell::new(delegate)); let delegate = Rc::new(RefCell::new(delegate));