Skip to content

Commit

Permalink
Allow switch input method when show Wox window
Browse files Browse the repository at this point in the history
  • Loading branch information
qianlifeng committed Oct 30, 2023
1 parent cd9dce6 commit 9a5876e
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 12 deletions.
5 changes: 4 additions & 1 deletion Wox.UI.Tauri/src-tauri/src/websocket.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use serde_json::Value;
use std::thread;
use std::time::Duration;
use serde_json::Value;
use tauri::{LogicalPosition, Position, Window};
use tungstenite::{connect, Message};
use url::Url;

use crate::get_server_port;

pub(crate) fn conn(window: Window) {
Expand Down Expand Up @@ -59,6 +60,8 @@ fn handle_show_message(window: &Window, v: &Value) -> Result<(), Box<dyn std::er
window.show()?;
window.set_focus()?;

window.eval(&format!("window.postShow()"))?;

if let Some(data) = v.get("Data") {
if let Some(position) = data.get("Position") {
if let (Some(x), Some(y), Some(type_position)) = (
Expand Down
9 changes: 9 additions & 0 deletions Wox.UI.Tauri/src/components/WoxLauncher.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ export default () => {
*/
const hideWoxWindow = async () => {
await WoxTauriHelper.getInstance().hideWindow()
await WoxMessageHelper.getInstance().sendMessage(WoxMessageMethodEnum.ON_VISIBILITY_CHANGED.code, {
"isVisible": "false"
})
}

/*
Expand Down Expand Up @@ -169,6 +172,12 @@ export default () => {
window.selectAll = () => {
woxQueryBoxRef.current?.selectAll()
}
// @ts-ignore expose to tauri backend
window.postShow = () => {
WoxMessageHelper.getInstance().sendMessage(WoxMessageMethodEnum.ON_VISIBILITY_CHANGED.code, {
"isVisible": "true"
})
}
}, [])

return <Style className={"wox-launcher"}>
Expand Down
13 changes: 7 additions & 6 deletions Wox.UI.Tauri/src/enums/WoxMessageMethodEnum.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import {BaseEnum} from "./base/BaseEnum.ts";
import { BaseEnum } from "./base/BaseEnum.ts"

export class WoxMessageMethodEnum extends BaseEnum {
static readonly PING = WoxMessageMethodEnum.define("Ping", "Ping");
static readonly QUERY = WoxMessageMethodEnum.define("Query", "Query");
static readonly ACTION = WoxMessageMethodEnum.define("Action", "Action");
static readonly REFRESH = WoxMessageMethodEnum.define("Refresh", "Refresh");
static readonly REGISTERMAINHOTKEY = WoxMessageMethodEnum.define("RegisterMainHotkey", "Register Main Hotkey");
static readonly PING = WoxMessageMethodEnum.define("Ping", "Ping")
static readonly QUERY = WoxMessageMethodEnum.define("Query", "Query")
static readonly ACTION = WoxMessageMethodEnum.define("Action", "Action")
static readonly REFRESH = WoxMessageMethodEnum.define("Refresh", "Refresh")
static readonly REGISTER_MAIN_HOTKEY = WoxMessageMethodEnum.define("RegisterMainHotkey", "Register Main Hotkey")
static readonly ON_VISIBILITY_CHANGED = WoxMessageMethodEnum.define("OnVisibilityChanged", "Visibility changed")
}
2 changes: 1 addition & 1 deletion Wox/plugin/preview.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ type WoxPreviewType = string
const (
WoxPreviewTypeMarkdown = "markdown"
WoxPreviewTypeText = "text"
WoxPreviewTypeImage = "image"
WoxPreviewTypeImage = "image" // when type is image, data should be WoxImage serialized with json
WoxPreviewTypeUrl = "url"
)

Expand Down
16 changes: 13 additions & 3 deletions Wox/plugin/system/clipboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,8 @@ func (c *ClipboardPlugin) Init(ctx context.Context, initParams plugin.InitParams
util.ClipboardWatch(func(data util.ClipboardData) {
c.api.Log(ctx, fmt.Sprintf("clipboard data changed, type=%s", data.Type))

icon := plugin.NewWoxImageSvg(`<svg xmlns="http://www.w3.org/2000/svg" x="0px" y="0px" width="48" height="48" viewBox="0 0 48 48"><path fill="#90CAF9" d="M40 45L8 45 8 3 30 3 40 13z"></path><path fill="#E1F5FE" d="M38.5 14L29 14 29 4.5z"></path><path fill="#1976D2" d="M16 21H33V23H16zM16 25H29V27H16zM16 29H33V31H16zM16 33H29V35H16z"></path></svg>`)
iconFilePath, iconErr := c.getActiveWindowIconFilePath(ctx)
if iconErr == nil {
icon := c.getDefaultTextIcon()
if iconFilePath, iconErr := c.getActiveWindowIconFilePath(ctx); iconErr == nil {
icon = plugin.NewWoxImageAbsolutePath(iconFilePath)
}

Expand Down Expand Up @@ -121,6 +120,13 @@ func (c *ClipboardPlugin) Query(ctx context.Context, query plugin.Query) []plugi

func (c *ClipboardPlugin) convertClipboardData(ctx context.Context, history ClipboardHistory) plugin.QueryResult {
if history.Data.Type == util.ClipboardTypeText {
if history.Icon.ImageType == plugin.WoxImageTypeAbsolutePath {
// if image doesn't exist, use default icon
if _, err := os.Stat(history.Icon.ImageData); err != nil {
history.Icon = c.getDefaultTextIcon()
}
}

return plugin.QueryResult{
Title: string(history.Data.Data),
Icon: history.Icon,
Expand Down Expand Up @@ -225,3 +231,7 @@ func (c *ClipboardPlugin) loadHistory(ctx context.Context) {
c.api.Log(ctx, fmt.Sprintf("load clipboard history, count=%d", len(history)))
c.history = history
}

func (c *ClipboardPlugin) getDefaultTextIcon() plugin.WoxImage {
return plugin.NewWoxImageSvg(`<svg xmlns="http://www.w3.org/2000/svg" x="0px" y="0px" width="48" height="48" viewBox="0 0 48 48"><path fill="#90CAF9" d="M40 45L8 45 8 3 30 3 40 13z"></path><path fill="#E1F5FE" d="M38.5 14L29 14 29 4.5z"></path><path fill="#1976D2" d="M16 21H33V23H16zM16 25H29V27H16zM16 29H33V31H16zM16 33H29V35H16z"></path></svg>`)
}
18 changes: 17 additions & 1 deletion Wox/util/input_source_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ package util
#include <stdio.h>
#include <CoreServices/CoreServices.h>
char* getCurrentInputMethod() {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
TISInputSourceRef source = TISCopyCurrentKeyboardInputSource();
CFStringRef sourceID = TISGetInputSourceProperty(source, kTISPropertyInputSourceID);
NSString *inputMethodID = (__bridge NSString *)sourceID;
[pool release];
return (char *)[inputMethodID UTF8String];
}
void switchInputMethod(const char *inputMethodID) {
CFStringRef inputMethodIDString = CFStringCreateWithCString(NULL, inputMethodID, kCFStringEncodingUTF8);
Expand All @@ -34,7 +43,14 @@ import (
)

func SwitchInputMethodABC() {
inputMethodIDStr := C.CString("com.apple.keylayout.ABC")
abcInputMethodID := "com.apple.keylayout.ABC"

inputMethod := C.GoString(C.getCurrentInputMethod())
if inputMethod == abcInputMethodID {
return
}

inputMethodIDStr := C.CString(abcInputMethodID)
defer C.free(unsafe.Pointer(inputMethodIDStr))
C.switchInputMethod(inputMethodIDStr)
}

1 comment on commit 9a5876e

@zergneptune
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

留下了感动的泪水,终于更新了

Please sign in to comment.