Skip to content

Commit

Permalink
feat: 重构并添加文档
Browse files Browse the repository at this point in the history
  • Loading branch information
faker2048 committed Jan 14, 2025
1 parent 606960f commit dc7bc4e
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 39 deletions.
33 changes: 33 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# WinResSelector

一款简洁优雅的 Windows 分辨率快速切换工具。

A sleek Windows resolution switcher utility.

## ✨ 特性 | Features

- 🎯 快速切换显示器分辨率 | Quick resolution switching
- 💾 保存常用分辨率配置 | Save frequently used resolution profiles
- 🔄 系统托盘快捷切换 | Quick access from system tray
- 🎨 现代化界面设计 | Modern UI design
- 🚀 轻量级且高效 | Lightweight and efficient

## 🖼️ 预览 | Preview

![主窗口截图](image.png)

![托盘图标截图](tray.png)

## 🚀 开始使用 | Getting Started

1.[Releases](https://github.com/fiko/WinResSelector/releases) 下载最新版本
2. 运行程序,添加您常用的分辨率配置
3. 可选择最小化到系统托盘,随时快速切换

Download the latest release, run the program, and add your frequently used resolution profiles. Optionally minimize to system tray for quick access.

## 📄 开源协议 | License

本项目基于 MIT 协议开源 - 查看 [LICENSE](LICENSE) 了解更多详情。

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
7 changes: 1 addition & 6 deletions WinResSelector/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,7 @@ namespace WinResSelector
{
public partial class App : Application
{
public IServiceProvider Services { get; }

public App()
{
Services = ConfigureServices();
}
public IServiceProvider Services { get; } = ConfigureServices();

private static IServiceProvider ConfigureServices()
{
Expand Down
1 change: 0 additions & 1 deletion WinResSelector/Models/DisplayProfile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ public override string ToString()

public class AppSettings
{
public bool StartWithWindows { get; init; }
public bool MinimizeToTray { get; init; }
}
}
8 changes: 4 additions & 4 deletions WinResSelector/Services/ConfigService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class ConfigService

private class Config
{
public List<DisplayProfile> Profiles { get; set; } = new();
public List<DisplayProfile?> Profiles { get; set; } = new();
public AppSettings Settings { get; set; } = new();
}

Expand Down Expand Up @@ -47,17 +47,17 @@ public void LoadConfig()
_isDirty = false;
}

public List<DisplayProfile> GetProfiles()
public List<DisplayProfile?> GetProfiles()
{
return _config?.Profiles ?? new List<DisplayProfile>();
return _config?.Profiles ?? new List<DisplayProfile?>();
}

public AppSettings GetSettings()
{
return _config?.Settings ?? new AppSettings();
}

public void SaveProfiles(List<DisplayProfile> profiles)
public void SaveProfiles(List<DisplayProfile?> profiles)
{
if (_config == null) _config = new Config();
_config.Profiles = profiles;
Expand Down
3 changes: 0 additions & 3 deletions WinResSelector/View/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,6 @@
Foreground="#B9BBBE"
FontSize="12"
Margin="0,0,0,8"/>
<CheckBox Content="开机启动"
IsChecked="{Binding StartWithWindows}"
Margin="0,4"/>
<CheckBox Content="最小化到托盘"
IsChecked="{Binding MinimizeToTray}"
Margin="0,4"/>
Expand Down
43 changes: 18 additions & 25 deletions WinResSelector/ViewModels/MainViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Threading.Tasks;
using System.Windows.Media;
Expand All @@ -25,14 +26,11 @@ public partial class MainViewModel : ObservableObject

[ObservableProperty]
private Brush _statusMessageColor = Brushes.Black;

[ObservableProperty]
private bool _startWithWindows;


[ObservableProperty]
private bool _minimizeToTray;

public ObservableCollection<DisplayProfile> Profiles { get; }
public ObservableCollection<DisplayProfile?> Profiles { get; }
public ObservableCollection<DisplaySettings> AvailableResolutions { get; }

public MainViewModel(ConfigService configService, DisplayService displayService,
Expand All @@ -43,7 +41,7 @@ public MainViewModel(ConfigService configService, DisplayService displayService,
_showWindow = showWindow;
_closeWindow = closeWindow;

Profiles = new ObservableCollection<DisplayProfile>();
Profiles = new ObservableCollection<DisplayProfile?>();
AvailableResolutions = new ObservableCollection<DisplaySettings>();

LoadData();
Expand Down Expand Up @@ -74,14 +72,13 @@ private void SaveSettings()
{
_configService.SaveSettings(new Models.AppSettings
{
StartWithWindows = StartWithWindows,
MinimizeToTray = MinimizeToTray
});
}

private void SaveProfiles()
{
_configService.SaveProfiles(new System.Collections.Generic.List<DisplayProfile>(Profiles));
_configService.SaveProfiles(new List<DisplayProfile?>(Profiles));
}

[RelayCommand]
Expand All @@ -96,16 +93,14 @@ private void AddProfile()
}

[RelayCommand]
private void DeleteProfile(DisplayProfile profile)
private void DeleteProfile(DisplayProfile? profile)
{
if (profile != null)
if (profile == null) return;
Profiles.Remove(profile);
// 重新排序 ID
for (var i = 0; i < Profiles.Count; i++)
{
Profiles.Remove(profile);
// 重新排序 ID
for (int i = 0; i < Profiles.Count; i++)
{
Profiles[i].Id = i + 1;
}
Profiles[i]!.Id = i + 1;
}
}

Expand Down Expand Up @@ -135,16 +130,14 @@ private void Exit()

private void ApplyProfile(DisplayProfile profile)
{
if (profile?.Display != null)
if (profile?.Display == null) return;
var success = _displayService.ChangeResolution(profile.Display);
if (!success)
{
bool success = _displayService.ChangeResolution(profile.Display);
if (!success)
{
StatusMessage = "分辨率切换失败";
StatusMessageColor = Brushes.Red;
}
UpdateCurrentResolution();
StatusMessage = "分辨率切换失败";
StatusMessageColor = Brushes.Red;
}
UpdateCurrentResolution();
}

private void LoadData()
Expand All @@ -160,13 +153,13 @@ private void LoadData()
// 再加载配置
_configService.LoadConfig();
var settings = _configService.GetSettings();
StartWithWindows = settings.StartWithWindows;
MinimizeToTray = settings.MinimizeToTray;

var profiles = _configService.GetProfiles();
Profiles.Clear();
foreach (var profile in profiles)
{
if (profile == null) continue;
// 查找匹配的分辨率
var matchingResolution = AvailableResolutions.FirstOrDefault(r =>
r.Width == profile.Display.Width &&
Expand Down
Binary file added image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tray.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit dc7bc4e

Please sign in to comment.