espanso/src/ui/macos.rs

123 lines
4.1 KiB
Rust
Raw Normal View History

2019-09-06 14:06:41 +00:00
use std::fs::create_dir_all;
2019-09-09 13:15:01 +00:00
use std::{fs, io};
2019-09-13 09:55:42 +00:00
use std::io::{Cursor};
2019-09-13 12:35:46 +00:00
use std::ffi::CString;
2019-09-13 09:55:42 +00:00
use log::{info, debug};
2019-09-09 13:15:01 +00:00
use std::path::PathBuf;
use std::process::Command;
2019-09-13 12:35:46 +00:00
use crate::ui::{MenuItem, MenuItemType};
use crate::bridge::macos::{MacMenuItem, show_context_menu};
use std::os::raw::c_char;
2019-09-06 14:06:41 +00:00
2019-09-09 13:15:01 +00:00
const NOTIFY_HELPER_BINARY : &'static [u8] = include_bytes!("../res/mac/EspansoNotifyHelper.zip");
const DEFAULT_NOTIFICATION_DELAY : f64 = 1.5;
2019-09-06 14:06:41 +00:00
pub struct MacUIManager {
2019-09-09 13:15:01 +00:00
notify_helper_path: PathBuf
2019-09-06 14:06:41 +00:00
}
impl super::UIManager for MacUIManager {
fn notify(&self, message: &str) {
2019-09-09 13:15:01 +00:00
let executable_path = self.notify_helper_path.join("Contents");
let executable_path = executable_path.join("MacOS");
let executable_path = executable_path.join("EspansoNotifyHelper");
let res = Command::new(executable_path)
.args(&["espanso", message, &DEFAULT_NOTIFICATION_DELAY.to_string()])
.spawn();
2019-09-06 14:06:41 +00:00
}
2019-09-13 09:55:42 +00:00
fn show_menu(&self, menu: Vec<MenuItem>) {
2019-09-13 12:35:46 +00:00
let mut raw_menu = Vec::new();
for item in menu.iter() {
let text = CString::new(item.item_name.clone()).unwrap_or_default();
let mut str_buff : [c_char; 100] = [0; 100];
unsafe {
std::ptr::copy(text.as_ptr(), str_buff.as_mut_ptr(), item.item_name.len());
}
let menu_type = match item.item_type {
MenuItemType::Button => {1},
MenuItemType::Separator => {2},
};
let raw_item = MacMenuItem {
item_id: item.item_id,
item_type: menu_type,
item_name: str_buff,
};
raw_menu.push(raw_item);
}
unsafe { show_context_menu(raw_menu.as_ptr(), raw_menu.len() as i32); }
2019-09-13 09:55:42 +00:00
}
2019-09-06 14:06:41 +00:00
}
impl MacUIManager {
2019-09-09 13:15:01 +00:00
pub fn new() -> MacUIManager {
let notify_helper_path = MacUIManager::initialize_notify_helper();
MacUIManager{
notify_helper_path
}
}
fn initialize_notify_helper() -> PathBuf {
let data_dir = dirs::data_dir().expect("Can't obtain data_dir(), terminating.");
2019-09-06 14:06:41 +00:00
2019-09-09 13:15:01 +00:00
let espanso_dir = data_dir.join("espanso");
2019-09-06 14:06:41 +00:00
2019-09-09 13:15:01 +00:00
let res = create_dir_all(&espanso_dir);
info!("Initializing EspansoNotifyHelper in {}", espanso_dir.as_path().display());
let espanso_target = espanso_dir.join("EspansoNotifyHelper.app");
if espanso_target.exists() {
info!("EspansoNotifyHelper already initialized, skipping.");
}else{
2019-09-06 14:06:41 +00:00
if let Ok(_) = res {
2019-09-09 13:15:01 +00:00
// Extract zip file
let reader = Cursor::new(NOTIFY_HELPER_BINARY);
let mut archive = zip::ZipArchive::new(reader).unwrap();
for i in 0..archive.len() {
let mut file = archive.by_index(i).unwrap();
let outpath = espanso_dir.join(file.sanitized_name());
{
let comment = file.comment();
if !comment.is_empty() {
debug!("File {} comment: {}", i, comment);
}
}
if (&*file.name()).ends_with('/') {
debug!("File {} extracted to \"{}\"", i, outpath.as_path().display());
fs::create_dir_all(&outpath).unwrap();
} else {
debug!("File {} extracted to \"{}\" ({} bytes)", i, outpath.as_path().display(), file.size());
if let Some(p) = outpath.parent() {
if !p.exists() {
fs::create_dir_all(&p).unwrap();
}
}
let mut outfile = fs::File::create(&outpath).unwrap();
io::copy(&mut file, &mut outfile).unwrap();
}
use std::os::unix::fs::PermissionsExt;
2019-09-06 14:06:41 +00:00
2019-09-09 13:15:01 +00:00
if let Some(mode) = file.unix_mode() {
fs::set_permissions(&outpath, fs::Permissions::from_mode(mode)).unwrap();
}
}
2019-09-06 14:06:41 +00:00
}
}
2019-09-09 13:15:01 +00:00
espanso_target
2019-09-06 14:06:41 +00:00
}
}