2021-04-16 10:13:59 +10:00
|
|
|
use std::sync::RwLock;
|
|
|
|
|
2022-07-16 00:14:02 +10:00
|
|
|
use cacao::uikit::{App, AppDelegate, Scene, SceneConfig, SceneConnectionOptions, SceneSession, Window, WindowSceneDelegate};
|
2021-04-16 10:13:59 +10:00
|
|
|
|
|
|
|
use cacao::color::Color;
|
2022-08-21 15:04:47 +10:00
|
|
|
use cacao::image::{Image, ImageView};
|
2021-04-16 10:13:59 +10:00
|
|
|
use cacao::layout::{Layout, LayoutConstraint};
|
|
|
|
use cacao::view::{View, ViewController, ViewDelegate};
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
struct TestApp;
|
|
|
|
|
|
|
|
impl AppDelegate for TestApp {
|
|
|
|
fn config_for_scene_session(&self, session: SceneSession, _options: SceneConnectionOptions) -> SceneConfig {
|
|
|
|
SceneConfig::new("Default Configuration", session.role())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct RootView {
|
|
|
|
pub red: View,
|
|
|
|
pub green: View,
|
2022-08-21 15:03:56 +10:00
|
|
|
pub blue: View,
|
2022-08-26 12:13:36 +10:00
|
|
|
pub image: ImageView,
|
2021-04-16 10:13:59 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ViewDelegate for RootView {
|
|
|
|
const NAME: &'static str = "RootView";
|
|
|
|
|
|
|
|
fn did_load(&mut self, view: View) {
|
|
|
|
self.red.set_background_color(Color::SystemRed);
|
|
|
|
self.red.layer.set_corner_radius(16.);
|
|
|
|
view.add_subview(&self.red);
|
|
|
|
|
|
|
|
self.green.set_background_color(Color::SystemGreen);
|
|
|
|
view.add_subview(&self.green);
|
|
|
|
|
|
|
|
self.blue.set_background_color(Color::SystemBlue);
|
|
|
|
view.add_subview(&self.blue);
|
|
|
|
|
2022-08-21 15:03:56 +10:00
|
|
|
let image_bytes = include_bytes!("../../test-data/favicon.ico");
|
|
|
|
self.image = ImageView::new();
|
|
|
|
self.image.set_image(&Image::with_data(image_bytes));
|
|
|
|
view.add_subview(&self.image);
|
|
|
|
|
2021-04-16 10:13:59 +10:00
|
|
|
LayoutConstraint::activate(&[
|
|
|
|
self.red.top.constraint_equal_to(&view.top).offset(16.),
|
|
|
|
self.red.leading.constraint_equal_to(&view.leading).offset(16.),
|
|
|
|
self.red.trailing.constraint_equal_to(&view.trailing).offset(-16.),
|
|
|
|
self.red.height.constraint_equal_to_constant(100.),
|
|
|
|
self.green.top.constraint_equal_to(&self.red.bottom).offset(16.),
|
|
|
|
self.green.leading.constraint_equal_to(&view.leading).offset(16.),
|
|
|
|
self.green.trailing.constraint_equal_to(&view.trailing).offset(-16.),
|
|
|
|
self.green.height.constraint_equal_to_constant(120.),
|
|
|
|
self.blue.top.constraint_equal_to(&self.green.bottom).offset(16.),
|
|
|
|
self.blue.leading.constraint_equal_to(&view.leading).offset(16.),
|
|
|
|
self.blue.trailing.constraint_equal_to(&view.trailing).offset(-16.),
|
2022-08-26 12:13:36 +10:00
|
|
|
self.blue.bottom.constraint_equal_to(&view.bottom).offset(-16.),
|
2021-04-16 10:13:59 +10:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct WindowScene {
|
|
|
|
pub window: RwLock<Option<Window>>,
|
2022-08-26 12:13:36 +10:00
|
|
|
pub root_view_controller: RwLock<Option<ViewController<RootView>>>,
|
2021-04-16 10:13:59 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
impl WindowSceneDelegate for WindowScene {
|
2022-07-16 00:14:02 +10:00
|
|
|
fn will_connect(&self, scene: Scene, session: SceneSession, options: SceneConnectionOptions) {
|
2021-04-16 10:13:59 +10:00
|
|
|
let bounds = scene.get_bounds();
|
|
|
|
let mut window = Window::new(bounds);
|
|
|
|
window.set_window_scene(scene);
|
2022-07-11 01:15:29 +10:00
|
|
|
|
2021-04-16 10:13:59 +10:00
|
|
|
let root_view_controller = ViewController::new(RootView::default());
|
|
|
|
window.set_root_view_controller(&root_view_controller);
|
|
|
|
window.show();
|
2022-07-11 01:15:29 +10:00
|
|
|
|
2021-04-16 10:13:59 +10:00
|
|
|
{
|
|
|
|
let mut w = self.window.write().unwrap();
|
|
|
|
*w = Some(window);
|
|
|
|
|
|
|
|
let mut vc = self.root_view_controller.write().unwrap();
|
|
|
|
*vc = Some(root_view_controller);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2022-07-16 00:14:02 +10:00
|
|
|
App::new(TestApp::default(), || Box::new(WindowScene::default())).run();
|
2021-04-16 10:13:59 +10:00
|
|
|
}
|