feat: first cut at openai compatible server
This commit is contained in:
@@ -1,8 +1,16 @@
|
||||
use muda::accelerator::{Accelerator, Code, Modifiers};
|
||||
use muda::accelerator::{Accelerator, CMD_OR_CTRL, Code, Modifiers};
|
||||
use muda::{Menu, MenuEvent, MenuItem, PredefinedMenuItem, Submenu};
|
||||
|
||||
use crate::native_edit::EditCommand;
|
||||
|
||||
const PREFERENCES: &str = "preferences";
|
||||
const MODEL_MANAGER: &str = "model-manager";
|
||||
const UNDO: &str = "undo";
|
||||
const REDO: &str = "redo";
|
||||
const CUT: &str = "cut";
|
||||
const COPY: &str = "copy";
|
||||
const PASTE: &str = "paste";
|
||||
const SELECT_ALL: &str = "select-all";
|
||||
|
||||
pub(crate) struct NativeMenu {
|
||||
_menu: Menu,
|
||||
@@ -12,6 +20,7 @@ pub(crate) struct NativeMenu {
|
||||
pub(crate) enum NativeMenuEvent {
|
||||
Preferences,
|
||||
ModelManager,
|
||||
Edit(EditCommand),
|
||||
}
|
||||
|
||||
pub(crate) fn install() -> Result<NativeMenu, String> {
|
||||
@@ -34,6 +43,12 @@ pub(crate) fn install() -> Result<NativeMenu, String> {
|
||||
Code::KeyM,
|
||||
)),
|
||||
);
|
||||
let undo = edit_item(UNDO, "Undo", Code::KeyZ, None);
|
||||
let redo = edit_item(REDO, "Redo", Code::KeyZ, Some(Modifiers::SHIFT));
|
||||
let cut = edit_item(CUT, "Cut", Code::KeyX, None);
|
||||
let copy = edit_item(COPY, "Copy", Code::KeyC, None);
|
||||
let paste = edit_item(PASTE, "Paste", Code::KeyV, None);
|
||||
let select_all = edit_item(SELECT_ALL, "Select All", Code::KeyA, None);
|
||||
|
||||
application
|
||||
.append_items(&[
|
||||
@@ -51,13 +66,13 @@ pub(crate) fn install() -> Result<NativeMenu, String> {
|
||||
])
|
||||
.map_err(|error| error.to_string())?;
|
||||
edit.append_items(&[
|
||||
&PredefinedMenuItem::undo(None),
|
||||
&PredefinedMenuItem::redo(None),
|
||||
&undo,
|
||||
&redo,
|
||||
&PredefinedMenuItem::separator(),
|
||||
&PredefinedMenuItem::cut(None),
|
||||
&PredefinedMenuItem::copy(None),
|
||||
&PredefinedMenuItem::paste(None),
|
||||
&PredefinedMenuItem::select_all(None),
|
||||
&cut,
|
||||
©,
|
||||
&paste,
|
||||
&select_all,
|
||||
])
|
||||
.map_err(|error| error.to_string())?;
|
||||
window
|
||||
@@ -79,12 +94,47 @@ pub(crate) fn install() -> Result<NativeMenu, String> {
|
||||
|
||||
pub(crate) fn next_event() -> Option<NativeMenuEvent> {
|
||||
while let Ok(event) = MenuEvent::receiver().try_recv() {
|
||||
if event.id == PREFERENCES {
|
||||
return Some(NativeMenuEvent::Preferences);
|
||||
}
|
||||
if event.id == MODEL_MANAGER {
|
||||
return Some(NativeMenuEvent::ModelManager);
|
||||
}
|
||||
let event = match event.id.0.as_str() {
|
||||
PREFERENCES => NativeMenuEvent::Preferences,
|
||||
MODEL_MANAGER => NativeMenuEvent::ModelManager,
|
||||
UNDO => NativeMenuEvent::Edit(EditCommand::Undo),
|
||||
REDO => NativeMenuEvent::Edit(EditCommand::Redo),
|
||||
CUT => NativeMenuEvent::Edit(EditCommand::Cut),
|
||||
COPY => NativeMenuEvent::Edit(EditCommand::Copy),
|
||||
PASTE => NativeMenuEvent::Edit(EditCommand::Paste),
|
||||
SELECT_ALL => NativeMenuEvent::Edit(EditCommand::SelectAll),
|
||||
_ => continue,
|
||||
};
|
||||
return Some(event);
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
fn edit_item(
|
||||
id: &'static str,
|
||||
label: &'static str,
|
||||
code: Code,
|
||||
extra_modifier: Option<Modifiers>,
|
||||
) -> MenuItem {
|
||||
MenuItem::with_id(
|
||||
id,
|
||||
label,
|
||||
true,
|
||||
Some(Accelerator::new(
|
||||
Some(CMD_OR_CTRL | extra_modifier.unwrap_or_else(Modifiers::empty)),
|
||||
code,
|
||||
)),
|
||||
)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn edit_items_are_custom_menu_events() {
|
||||
let paste = edit_item(PASTE, "Paste", Code::KeyV, None);
|
||||
assert_eq!(paste.id().0, PASTE);
|
||||
assert!(paste.is_enabled());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user