2021-02-13 12:57:06 +11:00
|
|
|
//! This example showcases setting up a basic application and window, setting up some views to
|
|
|
|
//! work with autolayout, and some basic ways to handle colors.
|
2020-03-21 08:02:46 +11:00
|
|
|
|
2021-02-13 12:57:06 +11:00
|
|
|
use cacao::color::{Color, Theme};
|
2020-03-29 12:45:24 +11:00
|
|
|
use cacao::layout::{Layout, LayoutConstraint};
|
|
|
|
use cacao::view::View;
|
2020-03-30 16:33:51 +11:00
|
|
|
|
2021-08-08 15:31:48 +10:00
|
|
|
use cacao::appkit::menu::{Menu, MenuItem};
|
|
|
|
use cacao::appkit::window::{Window, WindowConfig, WindowDelegate};
|
2022-07-16 00:14:02 +10:00
|
|
|
use cacao::appkit::{App, AppDelegate};
|
2020-03-21 08:02:46 +11:00
|
|
|
|
|
|
|
struct BasicApp {
|
2022-08-27 00:40:31 +10:00
|
|
|
window: Window<AppWindow>
|
2020-03-21 08:02:46 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
impl AppDelegate for BasicApp {
|
|
|
|
fn did_finish_launching(&self) {
|
2021-08-08 15:31:48 +10:00
|
|
|
App::set_menu(vec![
|
2022-08-27 00:40:31 +10:00
|
|
|
Menu::new("", vec![
|
|
|
|
MenuItem::Services,
|
|
|
|
MenuItem::Separator,
|
|
|
|
MenuItem::Hide,
|
|
|
|
MenuItem::HideOthers,
|
|
|
|
MenuItem::ShowAll,
|
|
|
|
MenuItem::Separator,
|
|
|
|
MenuItem::Quit,
|
|
|
|
]),
|
2022-07-16 00:14:02 +10:00
|
|
|
Menu::new("File", vec![MenuItem::CloseWindow]),
|
|
|
|
Menu::new("View", vec![MenuItem::EnterFullScreen]),
|
2022-08-27 00:40:31 +10:00
|
|
|
Menu::new("Window", vec![
|
|
|
|
MenuItem::Minimize,
|
|
|
|
MenuItem::Zoom,
|
|
|
|
MenuItem::Separator,
|
|
|
|
MenuItem::new("Bring All to Front"),
|
|
|
|
]),
|
2021-08-08 15:31:48 +10:00
|
|
|
]);
|
|
|
|
|
2021-02-08 15:51:40 +11:00
|
|
|
App::activate();
|
2021-08-08 15:31:48 +10:00
|
|
|
|
2020-03-21 08:02:46 +11:00
|
|
|
self.window.show();
|
|
|
|
}
|
2021-08-08 15:31:48 +10:00
|
|
|
|
|
|
|
fn should_terminate_after_last_window_closed(&self) -> bool {
|
|
|
|
true
|
|
|
|
}
|
2020-03-21 08:02:46 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
struct AppWindow {
|
|
|
|
content: View,
|
|
|
|
blue: View,
|
|
|
|
red: View,
|
2022-08-27 00:40:31 +10:00
|
|
|
green: View
|
2020-03-21 08:02:46 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
impl WindowDelegate for AppWindow {
|
2021-02-08 15:51:40 +11:00
|
|
|
const NAME: &'static str = "WindowDelegate";
|
|
|
|
|
|
|
|
fn did_load(&mut self, window: Window) {
|
2020-03-21 08:02:46 +11:00
|
|
|
window.set_title("AutoLayout Example");
|
|
|
|
window.set_minimum_content_size(300., 300.);
|
|
|
|
|
2021-02-13 12:57:06 +11:00
|
|
|
let dynamic = Color::dynamic(|style| match (style.theme, style.contrast) {
|
|
|
|
(Theme::Dark, _) => Color::SystemGreen,
|
2022-08-27 00:40:31 +10:00
|
|
|
_ => Color::SystemRed
|
2021-02-13 12:57:06 +11:00
|
|
|
});
|
|
|
|
|
|
|
|
self.blue.set_background_color(Color::SystemBlue);
|
2021-03-27 10:25:57 +11:00
|
|
|
self.blue.layer.set_corner_radius(16.);
|
2020-03-21 08:02:46 +11:00
|
|
|
self.content.add_subview(&self.blue);
|
|
|
|
|
2021-02-13 12:57:06 +11:00
|
|
|
self.red.set_background_color(Color::SystemRed);
|
2020-03-21 08:02:46 +11:00
|
|
|
self.content.add_subview(&self.red);
|
|
|
|
|
2021-02-13 12:57:06 +11:00
|
|
|
self.green.set_background_color(dynamic);
|
2020-03-21 08:02:46 +11:00
|
|
|
self.content.add_subview(&self.green);
|
|
|
|
|
2020-03-30 16:33:51 +11:00
|
|
|
window.set_content_view(&self.content);
|
2020-03-21 08:02:46 +11:00
|
|
|
|
|
|
|
LayoutConstraint::activate(&[
|
2021-03-05 12:24:39 +11:00
|
|
|
self.blue.top.constraint_equal_to(&self.content.top).offset(46.),
|
2020-03-21 08:02:46 +11:00
|
|
|
self.blue.leading.constraint_equal_to(&self.content.leading).offset(16.),
|
|
|
|
self.blue.bottom.constraint_equal_to(&self.content.bottom).offset(-16.),
|
|
|
|
self.blue.width.constraint_equal_to_constant(100.),
|
2021-03-05 12:24:39 +11:00
|
|
|
self.red.top.constraint_equal_to(&self.content.top).offset(46.),
|
2020-03-21 08:02:46 +11:00
|
|
|
self.red.leading.constraint_equal_to(&self.blue.trailing).offset(16.),
|
|
|
|
self.red.bottom.constraint_equal_to(&self.content.bottom).offset(-16.),
|
2021-03-05 12:24:39 +11:00
|
|
|
self.green.top.constraint_equal_to(&self.content.top).offset(46.),
|
2020-03-21 08:02:46 +11:00
|
|
|
self.green.leading.constraint_equal_to(&self.red.trailing).offset(16.),
|
|
|
|
self.green.trailing.constraint_equal_to(&self.content.trailing).offset(-16.),
|
|
|
|
self.green.bottom.constraint_equal_to(&self.content.bottom).offset(-16.),
|
2022-08-27 00:40:31 +10:00
|
|
|
self.green.width.constraint_equal_to_constant(100.)
|
2020-03-21 08:02:46 +11:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2022-08-27 00:40:31 +10:00
|
|
|
App::new("com.test.window", BasicApp {
|
|
|
|
window: Window::with(WindowConfig::default(), AppWindow::default())
|
|
|
|
})
|
2022-07-16 00:14:02 +10:00
|
|
|
.run();
|
2020-03-21 08:02:46 +11:00
|
|
|
}
|