2021-04-13 11:17:36 +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::input::{TextField, TextFieldDelegate};
|
2022-07-16 00:14:02 +10:00
|
|
|
use cacao::layout::{Layout, LayoutConstraint};
|
2021-04-13 11:17:36 +10:00
|
|
|
use cacao::view::View;
|
|
|
|
|
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};
|
2021-04-13 11:17:36 +10:00
|
|
|
|
|
|
|
struct BasicApp {
|
2022-08-18 09:11:45 +10:00
|
|
|
window: Window<AppWindow>,
|
2021-04-13 11:17:36 +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]),
|
2022-08-18 09:11:45 +10:00
|
|
|
Menu::new(
|
|
|
|
"Edit",
|
|
|
|
vec![
|
|
|
|
MenuItem::Undo,
|
|
|
|
MenuItem::Redo,
|
|
|
|
MenuItem::Separator,
|
|
|
|
MenuItem::Cut,
|
|
|
|
MenuItem::Copy,
|
|
|
|
MenuItem::Paste,
|
|
|
|
MenuItem::Separator,
|
|
|
|
MenuItem::SelectAll,
|
|
|
|
],
|
|
|
|
),
|
2022-07-16 00:14:02 +10:00
|
|
|
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"),
|
|
|
|
],
|
|
|
|
),
|
2022-07-16 00:14:02 +10:00
|
|
|
Menu::new("Help", vec![]),
|
2021-04-13 11:17:36 +10:00
|
|
|
]);
|
|
|
|
|
|
|
|
App::activate();
|
|
|
|
self.window.show();
|
|
|
|
}
|
2021-08-08 15:31:48 +10:00
|
|
|
|
|
|
|
fn should_terminate_after_last_window_closed(&self) -> bool {
|
|
|
|
true
|
|
|
|
}
|
2021-04-13 11:17:36 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Default)]
|
|
|
|
pub struct ConsoleLogger;
|
|
|
|
|
|
|
|
impl TextFieldDelegate for ConsoleLogger {
|
|
|
|
const NAME: &'static str = "ConsoleLogger";
|
|
|
|
|
|
|
|
fn text_should_begin_editing(&self, value: &str) -> bool {
|
|
|
|
println!("Should begin with value: {}", value);
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
|
|
|
fn text_did_change(&self, value: &str) {
|
|
|
|
println!("Did change to: {}", value);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn text_did_end_editing(&self, value: &str) {
|
|
|
|
println!("Ended: {}", value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
struct AppWindow {
|
|
|
|
input: TextField<ConsoleLogger>,
|
2022-08-18 09:11:45 +10:00
|
|
|
content: View,
|
2021-04-13 11:17:36 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
impl AppWindow {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
AppWindow {
|
|
|
|
input: TextField::with(ConsoleLogger),
|
2022-08-18 09:11:45 +10:00
|
|
|
content: View::new(),
|
2021-04-13 11:17:36 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl WindowDelegate for AppWindow {
|
|
|
|
const NAME: &'static str = "WindowDelegate";
|
|
|
|
|
|
|
|
fn did_load(&mut self, window: Window) {
|
|
|
|
window.set_title("Input Logger Example");
|
|
|
|
window.set_minimum_content_size(300., 300.);
|
|
|
|
|
|
|
|
self.content.add_subview(&self.input);
|
|
|
|
window.set_content_view(&self.content);
|
|
|
|
|
|
|
|
LayoutConstraint::activate(&[
|
|
|
|
self.input.center_x.constraint_equal_to(&self.content.center_x),
|
|
|
|
self.input.center_y.constraint_equal_to(&self.content.center_y),
|
2022-08-18 09:11:45 +10:00
|
|
|
self.input.width.constraint_equal_to_constant(280.),
|
2021-04-13 11:17:36 +10:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2022-08-18 09:11:45 +10:00
|
|
|
App::new(
|
|
|
|
"com.test.window",
|
|
|
|
BasicApp {
|
|
|
|
window: Window::with(WindowConfig::default(), AppWindow::new()),
|
|
|
|
},
|
|
|
|
)
|
2022-07-16 00:14:02 +10:00
|
|
|
.run();
|
2021-04-13 11:17:36 +10:00
|
|
|
}
|