You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
import{screen}from"electron";functionloadWindowState(userData){constfile=join(userData,"window-state.json");try{consts=JSON.parse(readFileSync(file,"utf8"));if(!Number.isFinite(s.width)||!Number.isFinite(s.height))returnnull;if(s.width<520||s.height<600)returnnull;constvisible=screen.getAllDisplays().some((d)=>{consta=d.workArea;returns.x<a.x+a.width&&s.x+s.width>a.x&&s.y<a.y+a.height&&s.y+s.height>a.y;});returnvisible ? s : null;}catch{returnnull;}}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
现象
DSH Desktop 每次重启,窗口都回到 1280×820,位置也回到默认点。手动调整过的尺寸从来不保留。
直接原因
lib/main.js里主窗口尺寸是写死的:在
lib/main.js里搜索所有窗口几何相关调用,只有 5 处setBounds和 1 处getBounds:PlatformView.open/setBoundsfollowsetSize(960, height)没有任何一处和"上次退出时的窗口大小"有关。
再搜索这些字段:
isMaximized0 次、zoomLevel0 次、恢复窗口状态的写盘逻辑 0 次。所以严格来说不是"被重置",而是这个功能从来没实现过——每次都是从常量 1280×820 开始。
一个容易误判的细节
~/Library/Application Support/dsh-desktop/下有个window-state.json:{ "width": 1444, "height": 990, "x": 64, "y": 71, "isMaximized": false, "zoomLevel": 0 }字段结构看起来很标准,像是窗口记忆功能写的。但实际上:
app.asar和app.asar.unpacked,window-state这个字符串命中 0 次这是个孤儿文件:可能是早期版本写过、后来逻辑删了但文件留在磁盘上。改它不会产生任何效果。
(另外
zoomLevel在整包里也是 0 次命中——窗口级缩放同样没有实现,只是文件里留了个字段。)建议实现
大概三十行,围着
createWindow()加。1. 读取与校验
启动时读
window-state.json,但不能无条件信任——外接显示器拔掉后,记住的坐标可能落在已经不存在的屏幕上,窗口会飞到看不见的地方。至少要校验:
x/y至少有一部分落在某个显示器内(用screen.getAllDisplays()遍历,不能只看primaryDisplay)2. 恢复
只对主窗口生效(
primary === true),欢迎窗和弹窗不该继承:3. 保存
不要监听
resize/move就立刻写盘——拖窗口时每秒能触发上百次,会反复写同一个文件。正确做法是只记脏标记 + 防抖,退出或隐藏时真正落盘:
这里有个坑:窗口最大化时
getBounds()返回的是最大化后的尺寸,直接存下来,下次启动会变成一个铺满屏幕但"没最大化"的窗口。所以最大化时要存getNormalBounds()。影响面
这个改动不涉及渲染层,风险很低,纯增量。
All reactions