2021-04-09 10:54:32 +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.
|
|
|
|
|
2021-04-15 12:00:34 +10:00
|
|
|
use cacao::notification_center::Dispatcher;
|
2021-04-13 11:17:36 +10:00
|
|
|
use cacao::webview::{WebView, WebViewConfig, WebViewDelegate};
|
|
|
|
|
2021-08-08 15:31:48 +10:00
|
|
|
use cacao::appkit::menu::{Menu, MenuItem};
|
|
|
|
use cacao::appkit::toolbar::Toolbar;
|
|
|
|
use cacao::appkit::window::{Window, WindowConfig, WindowDelegate, WindowToolbarStyle};
|
2022-07-16 00:14:02 +10:00
|
|
|
use cacao::appkit::{App, AppDelegate};
|
2021-04-15 12:00:34 +10:00
|
|
|
|
|
|
|
mod toolbar;
|
|
|
|
use toolbar::BrowserToolbar;
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum Action {
|
|
|
|
Back,
|
|
|
|
Forwards,
|
2022-08-19 07:45:19 +10:00
|
|
|
Load(String)
|
2021-04-15 12:00:34 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Action {
|
|
|
|
pub fn dispatch(self) {
|
|
|
|
App::<BasicApp, Self>::dispatch_main(self);
|
|
|
|
}
|
|
|
|
}
|
2021-04-09 10:54:32 +10:00
|
|
|
|
|
|
|
struct BasicApp {
|
2022-08-19 07:45:19 +10:00
|
|
|
window: Window<AppWindow>
|
2021-04-09 10:54:32 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
impl AppDelegate for BasicApp {
|
|
|
|
fn did_finish_launching(&self) {
|
|
|
|
App::set_menu(vec![
|
2022-08-19 07:45:19 +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-19 07:45:19 +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-19 07:45:19 +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-09 10:54:32 +10:00
|
|
|
]);
|
|
|
|
|
|
|
|
App::activate();
|
|
|
|
self.window.show();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-15 12:00:34 +10:00
|
|
|
impl Dispatcher for BasicApp {
|
|
|
|
type Message = Action;
|
2021-04-13 11:17:36 +10:00
|
|
|
|
2021-04-15 12:00:34 +10:00
|
|
|
fn on_ui_message(&self, message: Self::Message) {
|
|
|
|
let window = self.window.delegate.as_ref().unwrap();
|
|
|
|
let webview = &window.content;
|
2021-04-13 11:17:36 +10:00
|
|
|
|
2021-04-15 12:00:34 +10:00
|
|
|
match message {
|
2022-07-16 00:14:02 +10:00
|
|
|
Action::Back => {
|
|
|
|
webview.go_back();
|
|
|
|
},
|
|
|
|
Action::Forwards => {
|
|
|
|
webview.go_forward();
|
|
|
|
},
|
|
|
|
Action::Load(url) => {
|
|
|
|
window.load_url(&url);
|
2022-08-19 07:45:19 +10:00
|
|
|
}
|
2021-04-13 11:17:36 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-09 10:54:32 +10:00
|
|
|
#[derive(Default)]
|
2021-04-13 11:17:36 +10:00
|
|
|
pub struct WebViewInstance;
|
|
|
|
|
|
|
|
impl WebViewDelegate for WebViewInstance {}
|
|
|
|
|
2021-04-09 10:54:32 +10:00
|
|
|
struct AppWindow {
|
2021-04-13 11:17:36 +10:00
|
|
|
toolbar: Toolbar<BrowserToolbar>,
|
2022-08-19 07:45:19 +10:00
|
|
|
content: WebView<WebViewInstance>
|
2021-04-13 11:17:36 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
impl AppWindow {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
AppWindow {
|
|
|
|
toolbar: Toolbar::new("com.example.BrowserToolbar", BrowserToolbar::new()),
|
2022-08-19 07:45:19 +10:00
|
|
|
content: WebView::with(WebViewConfig::default(), WebViewInstance::default())
|
2021-04-13 11:17:36 +10:00
|
|
|
}
|
|
|
|
}
|
2021-04-15 12:00:34 +10:00
|
|
|
|
|
|
|
pub fn load_url(&self, url: &str) {
|
|
|
|
self.toolbar.delegate.as_ref().unwrap().set_url(url);
|
|
|
|
self.content.load_url(url);
|
|
|
|
}
|
2021-04-09 10:54:32 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
impl WindowDelegate for AppWindow {
|
|
|
|
const NAME: &'static str = "WindowDelegate";
|
|
|
|
|
|
|
|
fn did_load(&mut self, window: Window) {
|
|
|
|
window.set_title("Browser Example");
|
2021-04-15 12:00:34 +10:00
|
|
|
window.set_autosave_name("CacaoBrowserExample");
|
2021-04-09 10:54:32 +10:00
|
|
|
window.set_minimum_content_size(400., 400.);
|
|
|
|
|
2021-04-13 11:17:36 +10:00
|
|
|
window.set_toolbar(&self.toolbar);
|
2021-04-09 10:54:32 +10:00
|
|
|
window.set_content_view(&self.content);
|
|
|
|
|
2021-04-15 12:00:34 +10:00
|
|
|
self.load_url("https://www.duckduckgo.com/");
|
2021-04-09 10:54:32 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2022-08-19 07:45:19 +10:00
|
|
|
App::new("com.test.window", BasicApp {
|
|
|
|
window: Window::with(
|
|
|
|
{
|
|
|
|
let mut config = WindowConfig::default();
|
|
|
|
|
|
|
|
// This flag is necessary for Big Sur to use the correct toolbar style.
|
|
|
|
config.toolbar_style = WindowToolbarStyle::Expanded;
|
|
|
|
|
|
|
|
config
|
|
|
|
},
|
|
|
|
AppWindow::new()
|
|
|
|
)
|
|
|
|
})
|
2022-07-16 00:14:02 +10:00
|
|
|
.run();
|
2021-04-09 10:54:32 +10:00
|
|
|
}
|