Skip to content

Commit

Permalink
新功能:记忆窗口位置(#44
Browse files Browse the repository at this point in the history
  • Loading branch information
hiroi-sora committed Nov 3, 2023
1 parent 02b8472 commit 05f00ba
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 11 deletions.
10 changes: 7 additions & 3 deletions UmiOCR-data/qt_res/qml/Configs/Configs.qml
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ Item {
}
break
// 无需检查
case "var": // 任意
case "file": // 文件
case "text": // 文本
case "hotkey": // 热键
Expand Down Expand Up @@ -287,8 +288,8 @@ Item {
function getValue(key) {
return valueDict[key]
}
// 设置值
function setValue(key, val, isupdateUI=false) {
// 设置值 键, 值, 是否刷新UI, 是否立刻写入本地(还是缓存写入)
function setValue(key, val, isupdateUI=false, saveNow=false) {
if(valueDict[key] === val) // 排除相同值
return
let res = onChangedFunc(key, val, valueDict[key]) // 触发函数,传入新值和旧值
Expand All @@ -298,7 +299,10 @@ Item {
}
valueDict[key] = val
if(originDict[key].save) { // 需要保存值
saveValue(key)
if(saveNow) // 立刻保存
settings.setValue(key, val)
else // 缓存保存
saveValue(key)
}
if(isupdateUI && compDict.hasOwnProperty(key)) { // 刷新UI
compDict[key].updateUI()
Expand Down
6 changes: 5 additions & 1 deletion UmiOCR-data/qt_res/qml/Configs/GlobalConfigs.qml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ Configs {
"onChanged": (val)=>{ theme.dataFontFamily = val },
},
"scale": {
"title": qsTr("界面大小比例"),
"title": qsTr("界面与文字大小"),
"default": 1,
"optionsList": [
[0.5, "50%"],
Expand Down Expand Up @@ -154,6 +154,10 @@ Configs {
],
},
"simpleNotificationType": utilsDicts.getSimpleNotificationType(true),
"geometry": { // 存放主窗位置大小,字符串格式 "x,y,w,h"
"type": "var",
"default": "", // 如: 300,20,500,300
},
},

// 截图
Expand Down
85 changes: 79 additions & 6 deletions UmiOCR-data/qt_res/qml/MainWindow/MainWindowManager.qml
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,92 @@ Item {
property var mScreen: mainWin.screen
property int mx: mainWin.x
property int my: mainWin.y
property int mw: mainWin.width
property int mh: mainWin.height
// 最小宽高
property int minW: 400
property int minH: 400

// ========================= 【保存量】 =========================

// 主窗口属性初始化
Component.onCompleted: {
loadGeometry() // 恢复上次大小位置
// 启动时可见
const visi = !qmlapp.globalConfigs.getValue("window.startupInvisible")
setVisibility(visi)
if(!visi) {
qmlapp.popup.simple(qsTr("欢迎使用 Umi-OCR"), qsTr("已启用后台模式,可通过快捷键使用功能。"))
}
// 检查屏幕位置,防止初始出界
checkScreen()
}

// ========================= 【记录窗口位置大小】 =========================


Connections {
target: mainWin
function onClosing() {
saveGeometry()
}
}
// 保存
function saveGeometry() {
let xywh = checkGeometry(mx, my, mw, mh)
xywh = xywh.join(",")
qmlapp.globalConfigs.setValue("window.geometry", xywh, false, true)
console.log("% 保存窗口位置", xywh)
}
// 读取
function loadGeometry() {
let xywh = qmlapp.globalConfigs.getValue("window.geometry")
xywh = xywh.split(",")
if(xywh.length < 4) {
console.log("% 未能读取窗口位置", xywh)
return
}
for(let i=0; i<4; i++)
xywh[i] = parseInt(xywh[i])
let [x, y, w, h] = checkGeometry(xywh[0], xywh[1], xywh[2], xywh[3])
mainWin.x = x
mainWin.y = y
mainWin.width = w
mainWin.height = h
let screenIndex = 0
for(let i=0, l=Qt.application.screens.length; i<l; i++) {
let s = Qt.application.screens[i]
if(x >= s.virtualX && x <= s.virtualX+s.width
&& y >= s.virtualY && y <= s.virtualY+s.height) {
screenIndex = i
break
}
}
mainWin.screen = Qt.application.screens[screenIndex]
console.log("% 读取窗口位置", x, y, w, h, screenIndex)
}
// 检查窗口位置,返回检查后的值
function checkGeometry(x, y, w, h) {
// 检查宽高
if(w > mScreen.desktopAvailableWidth)
w = mScreen.desktopAvailableWidth
else if(w < minW)
w = minW
if(h > mScreen.desktopAvailableHeight)
h = mScreen.desktopAvailableHeight
else if(h < minH)
h = minH
// 检查位置
if(x < mScreen.virtualX)
x = mScreen.virtualX
else if(x > mScreen.virtualX+mScreen.desktopAvailableWidth-w)
x = mScreen.virtualX+mScreen.desktopAvailableWidth-w
if(y < mScreen.virtualY+30) // +30防止标题栏出界
y = mScreen.virtualY+30
else if(y > mScreen.virtualY+mScreen.desktopAvailableHeight-h)
y = mScreen.virtualY+mScreen.desktopAvailableHeight-h
return [x, y, w, h]
}


// ========================= 【接口】 =========================

// 返回主窗口是否可见
Expand Down Expand Up @@ -59,15 +131,16 @@ Item {

// 退出主窗口
function quit() {
saveGeometry()
Qt.quit()
}

// 检查主窗口初始化屏幕位置,防止出界及过大
function checkScreen() {
if(mainWin.width > mScreen.desktopAvailableWidth)
mainWin.width = mScreen.desktopAvailableWidth
if(mainWin.height > mScreen.desktopAvailableHeight)
mainWin.height = mScreen.desktopAvailableHeight
if(mw > mScreen.desktopAvailableWidth)
mw = mScreen.desktopAvailableWidth
if(mh > mScreen.desktopAvailableHeight)
mh = mScreen.desktopAvailableHeight
if(mx < mScreen.virtualX) {
mainWin.x = mScreen.virtualX
}
Expand Down
2 changes: 1 addition & 1 deletion UmiOCR-data/qt_res/qml/MainWindow/SystemTray.qml
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ SystemTrayIcon {

MenuItem {
text: qsTr("退出 Umi-OCR")
onTriggered: Qt.quit()
onTriggered: qmlapp.mainWin.quit()
}
}

Expand Down

0 comments on commit 05f00ba

Please sign in to comment.