增加windows打包与linux打包,增加windows/linux快捷键

This commit is contained in:
Tlntin
2022-11-18 14:46:46 +08:00
parent 2d6f779c87
commit d5f901311e
9 changed files with 521 additions and 54 deletions

View File

@@ -1,22 +1,23 @@
// at the top of main.rs - that will prevent the console from showing
#![windows_subsystem = "windows"]
extern crate image;
use tauri_utils::config::{Config, WindowConfig};
#[cfg(target_os = "macos")]
use wry::application::platform::macos::WindowBuilderExtMacOS;
#[cfg(target_os = "windows")]
use wry::application::platform::windows::WindowBuilderExtWindows;
use wry::{
application::{
accelerator::{Accelerator, SysMods},
event::{Event, StartCause, WindowEvent},
event_loop::{ControlFlow, EventLoop},
keyboard::KeyCode,
menu::{MenuBar as Menu, MenuItem, MenuItemAttributes, MenuType},
window::{Fullscreen, Window, WindowBuilder, Icon},
},
webview::WebViewBuilder,
};
fn main() -> wry::Result<()> {
use wry::{
application::{
accelerator::{Accelerator, SysMods},
event::{Event, StartCause, WindowEvent},
event_loop::{ControlFlow, EventLoop},
keyboard::KeyCode,
menu::{MenuBar as Menu, MenuItem, MenuItemAttributes, MenuType},
window::{Fullscreen, Window, WindowBuilder},
},
webview::WebViewBuilder,
};
let mut menu_bar_menu = Menu::new();
let mut first_menu = Menu::new();
@@ -44,7 +45,6 @@ fn main() -> wry::Result<()> {
width,
height,
resizable,
transparent,
fullscreen,
..
} = get_windows_config().unwrap_or(WindowConfig::default());
@@ -58,13 +58,23 @@ fn main() -> wry::Result<()> {
None
})
.with_inner_size(wry::application::dpi::LogicalSize::new(width, height));
let icon_path = concat!(env!("CARGO_MANIFEST_DIR"), "/icons/icon.ico");
let icon = load_icon(std::path::Path::new(icon_path));
#[cfg(target_os = "windows")]
let window = common_window
.with_decorations(false)
.with_decorations(true)
.with_title("")
.with_window_icon(Some(icon))
.build(&event_loop)
.unwrap();
#[cfg(target_os = "linux")]
let window = common_window
.with_title("")
.build(&event_loop)
.with_menu(menu_bar_menu)
.unwrap();
#[cfg(target_os = "macos")]
let window = common_window
.with_fullsize_content_view(true)
@@ -89,13 +99,29 @@ fn main() -> wry::Result<()> {
webbrowser::open(&href).expect("no browser");
}
};
#[cfg(target_os = "windows")]
let webview = WebViewBuilder::new(window)?
.with_url(&url.to_string())?
.with_devtools(cfg!(feature = "devtools"))
.with_initialization_script(include_str!("pake.js"))
.with_ipc_handler(handler)
.build()?;
#[cfg(target_os = "linux")]
let webview = WebViewBuilder::new(window)?
.with_url(&url.to_string())?
.with_devtools(cfg!(feature = "devtools"))
.with_initialization_script(include_str!("pake.js"))
.with_ipc_handler(handler)
.build()?;
#[cfg(target_os = "macos")]
let webview = WebViewBuilder::new(window)?
.with_url(&url.to_string())?
.with_devtools(cfg!(feature = "devtools"))
.with_initialization_script(include_str!("pake-mac.js"))
.with_ipc_handler(handler)
.build()?;
#[cfg(feature = "devtools")] {
webview.open_devtools();
@@ -131,3 +157,17 @@ fn get_windows_config() -> Option<WindowConfig> {
config.tauri.windows.iter().next().cloned()
}
fn load_icon(path: &std::path::Path) -> Icon {
let (icon_rgba, icon_width, icon_height) = {
// alternatively, you can embed the icon in the binary through `include_bytes!` macro and use `image::load_from_memory`
let image = image::open(path)
.expect("Failed to open icon path")
.into_rgba8();
let (width, height) = image.dimensions();
let rgba = image.into_raw();
(rgba, width, height)
};
Icon::from_rgba(icon_rgba, icon_width, icon_height).expect("Failed to open icon")
}

175
src-tauri/src/pake-mac.js Normal file
View File

@@ -0,0 +1,175 @@
/**
* @typedef {string} KeyboardKey `event.key` 的代号,
* 见 <https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values>
* @typedef {() => void} OnKeyDown 使用者按下 ⌘ [KeyboardKey] 时应该执行的行为
* 以 Meta 键 (⌘) 为首的快捷键清单。
* 每个写在这里的 shortcuts 都会运行 {@link Event.preventDefault}.
* @type {Record<KeyboardKey, OnKeyDown>}
*/
const metaKeyShortcuts = {
'ArrowUp': () => scrollTo(0, 0),
'ArrowDown': () => scrollTo(0, document.body.scrollHeight),
'[': () => window.history.back(),
']': () => window.history.forward(),
'r': () => window.location.reload(),
'-': () => zoomOut(),
'=': () => zoomIn(),
'0': () => zoomCommon(() => '100%'),
}
window.addEventListener('DOMContentLoaded', (_event) => {
const style = document.createElement('style');
style.innerHTML = `
#page #footer-wrapper,
.drawing-board .toolbar .toolbar-action,
.c-swiper-container,
.download_entry,
.lang, .copyright,
.wwads-cn, .adsbygoogle,
#Bottom > div.content > div.inner,
#Rightbar .sep20:nth-of-type(5),
#Rightbar > div.box:nth-child(4),
#Main > div.box:nth-child(8) > div
#Wrapper > div.sep20,
#Main > div.box:nth-child(8),
#masthead-ad,
#Rightbar > div:nth-child(6) > div.sidebar_compliance {
display: none !important;
}
#page .main_header {
padding-top: 20px;
}
.panel.give_me .nav_view {
top: 154px !important;
}
.columns .column #header{
padding-top: 30px;
}
ytd-masthead>#container.style-scope.ytd-masthead {
padding-top: 12px !important;
}
.wrap.h1body-exist.max-container > div.menu-tocs > div.menu-btn{
top: 28px;
}
#Wrapper{
background-color: #F8F8F8 !important;
background-image:none !important;
}
#Top {
border-bottom: none;
}
.container-with-note #home, .container-with-note #switcher{
top: 30px;
}
.geist-page nav.dashboard_nav__PRmJv {
padding-top:10px;
}
.geist-page .submenu button{
margin-top:24px;
}
#pack-top-dom:active {
cursor: grabbing;
cursor: -webkit-grabbing;
}
#pack-top-dom{
position:fixed;
background:transparent;
top:0;
width: 100%;
height: 20px;
cursor: grab;
cursor: -webkit-grab;
z-index: 90000;
}
`;
document.head.append(style);
const topDom = document.createElement('div');
topDom.id = 'pack-top-dom';
document.body.appendChild(topDom);
const domEl = document.getElementById('pack-top-dom');
domEl.addEventListener('mousedown', (e) => {
if (e.buttons === 1 && e.detail !== 2) {
window.ipc.postMessage('drag_window');
}
});
domEl.addEventListener('touchstart', () => {
window.ipc.postMessage('drag_window');
});
domEl.addEventListener('dblclick', () => {
window.ipc.postMessage('fullscreen');
});
document.addEventListener('keyup', function (event) {
const preventDefault = (f) => {
event.preventDefault();
f();
};
if (event.metaKey && event.key in metaKeyShortcuts) {
preventDefault(metaKeyShortcuts[event.key]);
}
});
document.addEventListener('click', (e) => {
const origin = e.target.closest('a');
if (origin && origin.href) {
origin.target = '_self';
//额外处理下 twitter 的外跳,对于其他需要外跳的可以改这里成对应域名
const href = origin.href;
if(location.host === "twitter.com" && href.indexOf("twitter.com")===-1){
e.preventDefault();
window.ipc.postMessage(`open_browser:${href}`);
}
}
});
});
setDefaultZoom();
function setDefaultZoom() {
const htmlZoom = window.localStorage.getItem('htmlZoom');
if (htmlZoom) {
document.getElementsByTagName('html')[0].style.zoom = htmlZoom;
}
}
/**
* @param {(htmlZoom: string) => string} [zoomRule]
*/
function zoomCommon(zoomRule) {
const htmlZoom = window.localStorage.getItem('htmlZoom') || '100%';
const html = document.getElementsByTagName('html')[0];
const zoom = zoomRule(htmlZoom);
html.style.zoom = zoom;
window.localStorage.setItem('htmlZoom', zoom);
}
function zoomIn() {
zoomCommon((htmlZoom) =>
`${Math.min(parseInt(htmlZoom) + 10, 200)}%`
);
}
function zoomOut() {
zoomCommon((htmlZoom) =>
`${Math.max(parseInt(htmlZoom) - 10, 30)}%`
);
}

View File

@@ -1,19 +1,20 @@
/**
* @typedef {string} KeyboardKey `event.key` 的代号,
* 见 <https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values>
* @typedef {() => void} OnKeyDown 使用者按下 ⌘ [KeyboardKey] 时应该执行的行为
* 以 Meta 键 (⌘) 为首的快捷键清单。
* @typedef {() => void} OnKeyDown 使用者按下 [CtrlKey] 时应该执行的行为
* 以 Ctrl 键 为首的快捷键清单。
* 每个写在这里的 shortcuts 都会运行 {@link Event.preventDefault}.
* @type {Record<KeyboardKey, OnKeyDown>}
*/
const metaKeyShortcuts = {
const ctrlKeyShortcuts = {
'ArrowUp': () => scrollTo(0, 0),
'ArrowDown': () => scrollTo(0, document.body.scrollHeight),
'[': () => window.history.back(),
']': () => window.history.forward(),
'ArrowLeft': () => window.history.back(),
'ArrowRight': () => window.history.forward(),
'r': () => window.location.reload(),
'-': () => zoomOut(),
'=': () => zoomIn(),
'+': () => zoomIn(),
'0': () => zoomCommon(() => '100%'),
}
@@ -121,8 +122,8 @@ window.addEventListener('DOMContentLoaded', (_event) => {
f();
};
if (event.metaKey && event.key in metaKeyShortcuts) {
preventDefault(metaKeyShortcuts[event.key]);
if (event.ctrlKey && event.key in ctrlKeyShortcuts) {
preventDefault(ctrlKeyShortcuts[event.key]);
}
});