♻️ Refactor keybinding service
This commit is contained in:
@@ -35,9 +35,11 @@ func (s *KeyBindingService) ServiceStartup(ctx context.Context, options applicat
|
||||
// SyncKeyBindings 同步快捷键配置
|
||||
func (s *KeyBindingService) SyncKeyBindings(ctx context.Context) error {
|
||||
defaults := models.NewDefaultKeyBindings()
|
||||
definedKeys := make(map[models.KeyBindingKey]models.KeyBinding)
|
||||
// 使用 type + name 作为唯一键
|
||||
definedKeys := make(map[string]models.KeyBinding)
|
||||
for _, kb := range defaults {
|
||||
definedKeys[kb.Key] = kb
|
||||
key := string(kb.Type) + ":" + string(kb.Name)
|
||||
definedKeys[key] = kb
|
||||
}
|
||||
|
||||
// 获取数据库中已有的快捷键
|
||||
@@ -48,20 +50,38 @@ func (s *KeyBindingService) SyncKeyBindings(ctx context.Context) error {
|
||||
|
||||
existingKeys := make(map[string]bool)
|
||||
for _, kb := range existing {
|
||||
existingKeys[kb.Key] = true
|
||||
key := kb.Type + ":" + kb.Name
|
||||
existingKeys[key] = true
|
||||
}
|
||||
|
||||
// 批量添加缺失的快捷键
|
||||
var builders []*ent.KeyBindingCreate
|
||||
for key, kb := range definedKeys {
|
||||
if !existingKeys[string(key)] {
|
||||
if !existingKeys[key] {
|
||||
create := s.db.Client.KeyBinding.Create().
|
||||
SetKey(string(kb.Key)).
|
||||
SetCommand(kb.Command).
|
||||
SetEnabled(kb.Enabled)
|
||||
if kb.Extension != "" {
|
||||
create.SetExtension(string(kb.Extension))
|
||||
SetName(string(kb.Name)).
|
||||
SetType(string(kb.Type)).
|
||||
SetExtension(string(kb.Extension)).
|
||||
SetEnabled(kb.Enabled).
|
||||
SetPreventDefault(kb.PreventDefault)
|
||||
|
||||
// 设置快捷键字段
|
||||
if kb.Key != "" {
|
||||
create.SetKey(kb.Key)
|
||||
}
|
||||
if kb.Macos != "" {
|
||||
create.SetMacos(kb.Macos)
|
||||
}
|
||||
if kb.Windows != "" {
|
||||
create.SetWindows(kb.Windows)
|
||||
}
|
||||
if kb.Linux != "" {
|
||||
create.SetLinux(kb.Linux)
|
||||
}
|
||||
if kb.Scope != "" {
|
||||
create.SetScope(kb.Scope)
|
||||
}
|
||||
|
||||
builders = append(builders, create)
|
||||
}
|
||||
}
|
||||
@@ -74,7 +94,8 @@ func (s *KeyBindingService) SyncKeyBindings(ctx context.Context) error {
|
||||
// 批量删除废弃的快捷键(硬删除)
|
||||
var deleteIDs []int
|
||||
for _, kb := range existing {
|
||||
if _, ok := definedKeys[models.KeyBindingKey(kb.Key)]; !ok {
|
||||
key := kb.Type + ":" + kb.Name
|
||||
if _, ok := definedKeys[key]; !ok {
|
||||
deleteIDs = append(deleteIDs, kb.ID)
|
||||
}
|
||||
}
|
||||
@@ -89,16 +110,50 @@ func (s *KeyBindingService) SyncKeyBindings(ctx context.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetAllKeyBindings 获取所有快捷键
|
||||
func (s *KeyBindingService) GetAllKeyBindings(ctx context.Context) ([]*ent.KeyBinding, error) {
|
||||
return s.db.Client.KeyBinding.Query().All(ctx)
|
||||
// GetKeyBindings 根据类型获取快捷键
|
||||
func (s *KeyBindingService) GetKeyBindings(ctx context.Context, kbType models.KeyBindingType) ([]*ent.KeyBinding, error) {
|
||||
if kbType == models.Standard {
|
||||
// Standard 模式:只返回 type=standard 且 enabled=true
|
||||
return s.db.Client.KeyBinding.Query().
|
||||
Where(
|
||||
keybinding.Type(string(kbType)),
|
||||
keybinding.Enabled(true),
|
||||
).
|
||||
All(ctx)
|
||||
}
|
||||
|
||||
// Emacs 模式:获取所有 enabled=true 的快捷键
|
||||
allEnabled, err := s.db.Client.KeyBinding.Query().
|
||||
Where(keybinding.Enabled(true)).
|
||||
All(ctx)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("query enabled key bindings error: %w", err)
|
||||
}
|
||||
|
||||
// 构建 emacs 快捷键的 name 集合
|
||||
emacsNames := make(map[string]bool)
|
||||
for _, kb := range allEnabled {
|
||||
if kb.Type == string(models.Emacs) {
|
||||
emacsNames[kb.Name] = true
|
||||
}
|
||||
}
|
||||
|
||||
// 过滤:去掉与 emacs 冲突的 standard 快捷键
|
||||
var result []*ent.KeyBinding
|
||||
for _, kb := range allEnabled {
|
||||
// 如果是 standard 类型,且与 emacs 有 name 冲突,则跳过
|
||||
if kb.Type == string(models.Standard) && emacsNames[kb.Name] {
|
||||
continue
|
||||
}
|
||||
result = append(result, kb)
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// GetKeyBindingByKey 根据Key获取快捷键
|
||||
func (s *KeyBindingService) GetKeyBindingByKey(ctx context.Context, key string) (*ent.KeyBinding, error) {
|
||||
kb, err := s.db.Client.KeyBinding.Query().
|
||||
Where(keybinding.Key(key)).
|
||||
Only(ctx)
|
||||
// GetKeyBindingByID 根据ID获取快捷键
|
||||
func (s *KeyBindingService) GetKeyBindingByID(ctx context.Context, id int) (*ent.KeyBinding, error) {
|
||||
kb, err := s.db.Client.KeyBinding.Get(ctx, id)
|
||||
if err != nil {
|
||||
if ent.IsNotFound(err) {
|
||||
return nil, nil
|
||||
@@ -108,28 +163,41 @@ func (s *KeyBindingService) GetKeyBindingByKey(ctx context.Context, key string)
|
||||
return kb, nil
|
||||
}
|
||||
|
||||
// UpdateKeyBindingCommand 更新快捷键命令
|
||||
func (s *KeyBindingService) UpdateKeyBindingCommand(ctx context.Context, key string, command string) error {
|
||||
kb, err := s.GetKeyBindingByKey(ctx, key)
|
||||
// UpdateKeyBindingKeys 更新快捷键绑定(根据操作系统自动判断更新哪个字段)
|
||||
func (s *KeyBindingService) UpdateKeyBindingKeys(ctx context.Context, id int, key string) error {
|
||||
kb, err := s.GetKeyBindingByID(ctx, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if kb == nil {
|
||||
return fmt.Errorf("key binding not found: %s", key)
|
||||
return fmt.Errorf("key binding not found: id=%d", id)
|
||||
}
|
||||
return s.db.Client.KeyBinding.UpdateOneID(kb.ID).
|
||||
SetCommand(command).
|
||||
Exec(ctx)
|
||||
|
||||
update := s.db.Client.KeyBinding.UpdateOneID(kb.ID)
|
||||
|
||||
os := application.Get().Env.Info().OS
|
||||
switch os {
|
||||
case "darwin":
|
||||
update.SetMacos(key)
|
||||
case "windows":
|
||||
update.SetWindows(key)
|
||||
case "linux":
|
||||
update.SetLinux(key)
|
||||
default:
|
||||
s.logger.Error("unknown os: %s", os)
|
||||
}
|
||||
|
||||
return update.SetKey(key).Exec(ctx)
|
||||
}
|
||||
|
||||
// UpdateKeyBindingEnabled 更新快捷键启用状态
|
||||
func (s *KeyBindingService) UpdateKeyBindingEnabled(ctx context.Context, key string, enabled bool) error {
|
||||
kb, err := s.GetKeyBindingByKey(ctx, key)
|
||||
func (s *KeyBindingService) UpdateKeyBindingEnabled(ctx context.Context, id int, enabled bool) error {
|
||||
kb, err := s.GetKeyBindingByID(ctx, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if kb == nil {
|
||||
return fmt.Errorf("key binding not found: %s", key)
|
||||
return fmt.Errorf("key binding not found: id=%d", id)
|
||||
}
|
||||
return s.db.Client.KeyBinding.UpdateOneID(kb.ID).
|
||||
SetEnabled(enabled).
|
||||
@@ -140,3 +208,48 @@ func (s *KeyBindingService) UpdateKeyBindingEnabled(ctx context.Context, key str
|
||||
func (s *KeyBindingService) GetDefaultKeyBindings() []models.KeyBinding {
|
||||
return models.NewDefaultKeyBindings()
|
||||
}
|
||||
|
||||
// ResetKeyBindings 重置所有快捷键到默认值
|
||||
func (s *KeyBindingService) ResetKeyBindings(ctx context.Context) error {
|
||||
// 获取默认快捷键
|
||||
defaults := models.NewDefaultKeyBindings()
|
||||
|
||||
// 构建默认快捷键映射 (type:name -> KeyBinding)
|
||||
defaultsMap := make(map[string]models.KeyBinding)
|
||||
for _, kb := range defaults {
|
||||
key := string(kb.Type) + ":" + string(kb.Name)
|
||||
defaultsMap[key] = kb
|
||||
}
|
||||
|
||||
// 获取数据库中所有快捷键
|
||||
existing, err := s.db.Client.KeyBinding.Query().All(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("query key bindings error: %w", err)
|
||||
}
|
||||
|
||||
// 更新所有快捷键到默认值
|
||||
for _, existingKb := range existing {
|
||||
key := existingKb.Type + ":" + existingKb.Name
|
||||
defaultKb, ok := defaultsMap[key]
|
||||
if !ok {
|
||||
// 如果默认配置中没有这个快捷键,跳过
|
||||
continue
|
||||
}
|
||||
|
||||
// 无条件更新所有字段到默认值
|
||||
update := s.db.Client.KeyBinding.UpdateOneID(existingKb.ID).
|
||||
SetKey(defaultKb.Key).
|
||||
SetMacos(defaultKb.Macos).
|
||||
SetWindows(defaultKb.Windows).
|
||||
SetLinux(defaultKb.Linux).
|
||||
SetScope(defaultKb.Scope).
|
||||
SetEnabled(defaultKb.Enabled).
|
||||
SetPreventDefault(defaultKb.PreventDefault)
|
||||
|
||||
if err := update.Exec(ctx); err != nil {
|
||||
return fmt.Errorf("update key binding error: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user