add DPI Scaling setting

This commit is contained in:
josc146 2023-06-20 22:22:14 +08:00
parent ce915cdf6a
commit 285e8b1577
7 changed files with 77 additions and 8 deletions

View File

@ -2,6 +2,7 @@ package backend_golang
import (
"context"
"errors"
"net/http"
"os"
"os/exec"
@ -15,6 +16,8 @@ import (
// App struct
type App struct {
ctx context.Context
HasConfigData bool
ConfigData map[string]any
exDir string
cmdPrefix string
}
@ -64,6 +67,19 @@ func (a *App) UpdateApp(url string) (broken bool, err error) {
return false, nil
}
func (a *App) RestartApp() error {
if runtime.GOOS == "windows" {
name, err := os.Executable()
if err != nil {
return err
}
exec.Command(name, os.Args[1:]...).Start()
wruntime.Quit(a.ctx)
return nil
}
return errors.New("unsupported OS")
}
func (a *App) GetPlatform() string {
return runtime.GOOS
}

View File

@ -148,5 +148,8 @@
"Are you sure you want to clear the conversation? It cannot be undone.": "你确定要清空对话吗?这无法撤销",
"Save": "保存",
"Conversation Saved": "对话已保存",
"Open": "打开"
"Open": "打开",
"DPI Scaling": "显示缩放",
"Restart the app to apply DPI Scaling.": "重启应用以使显示缩放生效",
"Restart": "重启"
}

View File

@ -14,7 +14,8 @@ import { Labeled } from '../components/Labeled';
import commonStore from '../stores/commonStore';
import { observer } from 'mobx-react-lite';
import { useTranslation } from 'react-i18next';
import { checkUpdate } from '../utils';
import { checkUpdate, toastWithButton } from '../utils';
import { RestartApp } from '../../wailsjs/go/backend_golang/App';
export const Languages = {
dev: 'English', // i18n default
@ -30,6 +31,7 @@ export type SettingsType = {
giteeUpdatesSource: boolean
cnMirror: boolean
host: string
dpiScaling: number
customModelsPath: string
customPythonPath: string
}
@ -45,7 +47,7 @@ export const Settings: FC = observer(() => {
return (
<Page title={t('Settings')} content={
<div className="flex flex-col gap-2 overflow-hidden">
<div className="flex flex-col gap-2 overflow-y-auto overflow-x-hidden p-1">
<Labeled label={t('Language')} flex spaceBetween content={
<Dropdown style={{ minWidth: 0 }} listbox={{ style: { minWidth: 0 } }}
value={Languages[commonStore.settings.language]}
@ -56,7 +58,6 @@ export const Settings: FC = observer(() => {
commonStore.setSettings({
language: lang
});
i18n.changeLanguage(lang);
}
}}>
{
@ -65,6 +66,31 @@ export const Settings: FC = observer(() => {
}
</Dropdown>
} />
{
commonStore.platform === 'windows' &&
<Labeled label={t('DPI Scaling')} flex spaceBetween content={
<Dropdown style={{ minWidth: 0 }} listbox={{ style: { minWidth: 0 } }}
value={commonStore.settings.dpiScaling + '%'}
selectedOptions={[commonStore.settings.dpiScaling.toString()]}
onOptionSelect={(_, data) => {
if (data.optionValue) {
commonStore.setSettings({
dpiScaling: Number(data.optionValue)
});
toastWithButton(t('Restart the app to apply DPI Scaling.'), t('Restart'), () => {
RestartApp();
}, {
autoClose: 5000
});
}
}}>
{
Array.from({ length: 7 }, (_, i) => (i + 2) * 25).map((v, i) =>
<Option key={i} value={v.toString()}>{v + '%'}</Option>)
}
</Dropdown>
} />
}
<Labeled label={t('Dark Mode')} flex spaceBetween content={
<Switch checked={commonStore.settings.darkMode}
onChange={(e, data) => {

View File

@ -63,6 +63,7 @@ class CommonStore {
giteeUpdatesSource: getUserLanguage() === 'zh',
cnMirror: getUserLanguage() === 'zh',
host: '127.0.0.1',
dpiScaling: 100,
customModelsPath: './models',
customPythonPath: ''
};

View File

@ -34,6 +34,8 @@ export function ReadFileInfo(arg1:string):Promise<backend_golang.FileInfo>;
export function ReadJson(arg1:string):Promise<any>;
export function RestartApp():Promise<void>;
export function SaveJson(arg1:string,arg2:any):Promise<void>;
export function StartServer(arg1:string,arg2:number,arg3:string):Promise<string>;

View File

@ -66,6 +66,10 @@ export function ReadJson(arg1) {
return window['go']['backend_golang']['App']['ReadJson'](arg1);
}
export function RestartApp() {
return window['go']['backend_golang']['App']['RestartApp']();
}
export function SaveJson(arg1, arg2) {
return window['go']['backend_golang']['App']['SaveJson'](arg1, arg2);
}

19
main.go
View File

@ -11,6 +11,7 @@ import (
"github.com/wailsapp/wails/v2"
"github.com/wailsapp/wails/v2/pkg/options"
"github.com/wailsapp/wails/v2/pkg/options/assetserver"
"github.com/wailsapp/wails/v2/pkg/options/windows"
)
//go:embed all:frontend/dist
@ -36,13 +37,29 @@ func main() {
// Create an instance of the app structure
app := backend.NewApp()
var zoomFactor float64 = 1.0
data, err := app.ReadJson("config.json")
if err == nil {
app.HasConfigData = true
app.ConfigData = data.(map[string]any)
if dpiScaling, ok := app.ConfigData["settings"].(map[string]any)["dpiScaling"]; ok {
zoomFactor = dpiScaling.(float64) / 100
}
} else {
app.HasConfigData = false
}
// Create application with options
err := wails.Run(&options.App{
err = wails.Run(&options.App{
Title: "RWKV-Runner",
Width: 1024,
Height: 680,
MinWidth: 375,
MinHeight: 640,
Windows: &windows.Options{
ZoomFactor: zoomFactor,
IsZoomControlEnabled: true,
},
AssetServer: &assetserver.Options{
Assets: assets,
},