2021-08-09 11:42:07 +10: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.
|
|
|
|
|
|
|
|
use cacao::color::Color;
|
|
|
|
use cacao::geometry::Rect;
|
|
|
|
use cacao::layout::Layout;
|
|
|
|
use cacao::view::View;
|
|
|
|
|
|
|
|
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};
|
2021-08-09 11:42:07 +10:00
|
|
|
|
2022-01-07 18:25:36 +11:00
|
|
|
const CORNER_RADIUS: f64 = 16.;
|
|
|
|
const SPACING: f64 = 10.;
|
|
|
|
const TOP: f64 = 40.;
|
|
|
|
const WIDTH: f64 = 100.;
|
|
|
|
const HEIGHT: f64 = 100.;
|
|
|
|
|
2021-08-09 11:42:07 +10:00
|
|
|
struct BasicApp {
|
2022-08-18 09:11:45 +10:00
|
|
|
window: Window<AppWindow>,
|
2021-08-09 11:42:07 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
impl AppDelegate for BasicApp {
|
|
|
|
fn did_finish_launching(&self) {
|
|
|
|
App::set_menu(vec![
|
2022-08-18 09:11:45 +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-18 09:11:45 +10:00
|
|
|
Menu::new(
|
|
|
|
"Window",
|
|
|
|
vec![
|
|
|
|
MenuItem::Minimize,
|
|
|
|
MenuItem::Zoom,
|
|
|
|
MenuItem::Separator,
|
|
|
|
MenuItem::new("Bring All to Front"),
|
|
|
|
],
|
|
|
|
),
|
2021-08-09 11:42:07 +10:00
|
|
|
]);
|
|
|
|
|
|
|
|
App::activate();
|
|
|
|
|
|
|
|
self.window.show();
|
2022-01-07 18:25:36 +11:00
|
|
|
self.window.delegate.as_ref().unwrap().layout();
|
2021-08-09 11:42:07 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
fn should_terminate_after_last_window_closed(&self) -> bool {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
struct AppWindow {
|
|
|
|
content: View,
|
|
|
|
blue: View,
|
|
|
|
red: View,
|
2022-08-18 09:11:45 +10:00
|
|
|
green: View,
|
2021-08-09 11:42:07 +10:00
|
|
|
}
|
|
|
|
|
2022-01-07 18:25:36 +11:00
|
|
|
impl AppWindow {
|
|
|
|
pub fn layout(&self) {
|
2021-08-09 11:42:07 +10:00
|
|
|
self.blue.set_background_color(Color::SystemBlue);
|
|
|
|
self.blue.set_frame(Rect {
|
|
|
|
top: TOP,
|
|
|
|
left: SPACING,
|
|
|
|
width: WIDTH,
|
2022-08-18 09:11:45 +10:00
|
|
|
height: HEIGHT,
|
2021-08-09 11:42:07 +10:00
|
|
|
});
|
|
|
|
self.blue.layer.set_corner_radius(CORNER_RADIUS);
|
|
|
|
self.content.add_subview(&self.blue);
|
|
|
|
|
|
|
|
self.red.set_background_color(Color::SystemRed);
|
|
|
|
self.red.set_frame(Rect {
|
|
|
|
top: TOP,
|
|
|
|
left: WIDTH + (SPACING * 2.),
|
|
|
|
width: WIDTH,
|
2022-08-18 09:11:45 +10:00
|
|
|
height: HEIGHT,
|
2021-08-09 11:42:07 +10:00
|
|
|
});
|
|
|
|
self.red.layer.set_corner_radius(CORNER_RADIUS);
|
|
|
|
self.content.add_subview(&self.red);
|
|
|
|
|
|
|
|
self.green.set_background_color(Color::SystemGreen);
|
|
|
|
self.green.set_frame(Rect {
|
|
|
|
top: TOP,
|
|
|
|
left: (WIDTH * 2.) + (SPACING * 3.),
|
|
|
|
width: WIDTH,
|
2022-08-18 09:11:45 +10:00
|
|
|
height: HEIGHT,
|
2021-08-09 11:42:07 +10:00
|
|
|
});
|
|
|
|
self.green.layer.set_corner_radius(CORNER_RADIUS);
|
|
|
|
self.content.add_subview(&self.green);
|
2022-01-07 18:25:36 +11:00
|
|
|
}
|
|
|
|
}
|
2021-08-09 11:42:07 +10:00
|
|
|
|
2022-01-07 18:25:36 +11:00
|
|
|
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.);
|
2021-08-09 11:42:07 +10:00
|
|
|
window.set_content_view(&self.content);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2022-08-18 09:11:45 +10:00
|
|
|
App::new(
|
|
|
|
"com.test.window",
|
|
|
|
BasicApp {
|
|
|
|
window: Window::with(WindowConfig::default(), AppWindow::default()),
|
|
|
|
},
|
|
|
|
)
|
2022-07-16 00:14:02 +10:00
|
|
|
.run();
|
2021-08-09 11:42:07 +10:00
|
|
|
}
|