Skip to content

Commit

Permalink
game mode ctrl
Browse files Browse the repository at this point in the history
  • Loading branch information
kongbin committed Mar 4, 2025
1 parent e3c7085 commit 9b9b847
Show file tree
Hide file tree
Showing 7 changed files with 300 additions and 8 deletions.
15 changes: 15 additions & 0 deletions flutter/lib/common/widgets/toolbar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import 'package:flutter_hbb/models/model.dart';
import 'package:flutter_hbb/models/platform_model.dart';
import 'package:get/get.dart';

import '../../models/game_mode_state.dart';
const String kOptionGameMode = 'game-mode';

bool isEditOsPassword = false;

class TTextMenu {
Expand Down Expand Up @@ -235,6 +238,18 @@ List<TTextMenu> toolbarControls(BuildContext context, String id, FFI ffi) {
blockInput.value = !blockInput.value;
}));
}
// gameMode
if (ffi.ffiModel.keyboard && pi.platform == kPeerPlatformWindows) {
v.add(TTextMenu(
child: Obx(() => Text(translate('Game Mode'))),
onPressed: () {
bind.sessionToggleOption(
sessionId: sessionId, value: kOptionGameMode);
gameModeState.gameModeEnabled.value =
bind.sessionGetToggleOptionSync(
sessionId: sessionId, arg: kOptionGameMode);
}));
}
// switchSides
if (isDesktop &&
ffiModel.keyboard &&
Expand Down
28 changes: 28 additions & 0 deletions flutter/lib/desktop/widgets/remote_toolbar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import '../../models/platform_model.dart';
import '../../common/shared_state.dart';
import './popup_menu.dart';
import './kb_layout_type_chooser.dart';
import 'package:flutter_hbb/models/game_mode_state.dart';

class ToolbarState {
late RxBool _pin;
Expand Down Expand Up @@ -319,6 +320,33 @@ class RemoteMenuEntry {
dismissCallback: dismissCallback,
);
}

// 添加游戏模式菜单项
static MenuEntrySwitch<String> gameMode(
SessionID sessionId,
EdgeInsets padding, {
DismissFunc? dismissFunc,
DismissCallback? dismissCallback,
}) {
return MenuEntrySwitch<String>(
switchType: SwitchType.scheckbox,
text: translate('Game Mode'),
getter: () async {
return gameModeState.gameModeEnabled.value;
},
setter: (bool v) async {
await bind.sessionToggleOption(sessionId: sessionId, value: kOptionGameMode);
gameModeState.gameModeEnabled.value = bind.sessionGetToggleOptionSync(
sessionId: sessionId, arg: kOptionGameMode);
if (dismissFunc != null) {
dismissFunc();
}
},
padding: padding,
dismissOnClicked: true,
dismissCallback: dismissCallback,
);
}
}

class RemoteToolbar extends StatefulWidget {
Expand Down
3 changes: 3 additions & 0 deletions flutter/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import 'consts.dart';
import 'mobile/pages/home_page.dart';
import 'mobile/pages/server_page.dart';
import 'models/platform_model.dart';
import 'models/game_mode_state.dart';

import 'package:flutter_hbb/plugin/handlers.dart'
if (dart.library.html) 'package:flutter_hbb/web/plugin/handlers.dart';
Expand Down Expand Up @@ -526,6 +527,8 @@ _registerEventHandler() {
platformFFI.registerEventHandler('native_ui', 'native_ui', (evt) async {
NativeUiHandler.instance.onEvent(evt);
});
// 初始化游戏模式状态
gameModeState.init();
}
}

Expand Down
41 changes: 41 additions & 0 deletions flutter/lib/models/game_mode_state.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:flutter_hbb/common.dart';
import 'package:flutter_hbb/models/platform_model.dart';

// 游戏模式状态管理类
class GameModeState {
// 单例模式
static final GameModeState instance = GameModeState._internal();
GameModeState._internal();

// 游戏模式是否启用的状态
final RxBool gameModeEnabled = false.obs;

// 注册事件处理器,监听游戏模式状态变化
void registerEventListener() {
// 注册游戏模式状态变化事件
platformFFI.registerEventHandler('on_game_mode_changed', 'game_mode', (evt) async {
bool enabled = evt['enabled'] ?? false;
gameModeEnabled.value = enabled;
debugPrint('Game mode changed: $enabled');

// 显示游戏模式状态提示
if (enabled) {
showToast('游戏模式已启用');
} else {
showToast('游戏模式已禁用');
}
});
}

// 初始化游戏模式状态
void init() {
// 读取当前游戏模式状态
gameModeEnabled.value = bind.sessionGetOption(id: ''.obs, arg: kOptionGameMode) == 'Y';
registerEventListener();
}
}

// 全局访问点
final gameModeState = GameModeState.instance;
33 changes: 25 additions & 8 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1585,10 +1585,11 @@ impl LoginConfigHandler {
self.force_relay =
config::option2bool("force-always-relay", &self.get_option("force-always-relay"))
|| force_relay;
if let Some((real_id, server, key)) = &self.other_server {
let other_server_key = self.get_option("other-server-key");
if !other_server_key.is_empty() && key.is_empty() {
self.other_server = Some((real_id.to_owned(), server.to_owned(), other_server_key));
if let Some((real_id, server, key)) = self.other_server.as_ref() {
if server != PUBLIC_SERVER {
self.config
.options
.insert("other-server-key".to_owned(), key.clone());
}
}
self.direct = None;
Expand Down Expand Up @@ -1806,6 +1807,21 @@ impl LoginConfigHandler {
BoolOption::No
})
.into();
} else if name == "game-mode" {
// 处理游戏模式选项
let mut game_mode_enabled = self.get_toggle_option("game-mode");
game_mode_enabled = !game_mode_enabled;

if game_mode_enabled {
#[cfg(target_os = "windows")]
crate::game_mode::enable();
} else {
#[cfg(target_os = "windows")]
crate::game_mode::disable();
}

// 保存游戏模式状态
config.options.insert("game-mode".to_owned(), if game_mode_enabled { "Y".to_owned() } else { "N".to_owned() });
} else if name == "privacy-mode" {
// try toggle privacy mode
option.privacy_mode = (if config.privacy_mode.v {
Expand Down Expand Up @@ -3085,10 +3101,10 @@ pub async fn handle_hash(
interface.msgbox("input-password", "Password Required", "", "");
Vec::new()
} else {
let mut hasher = Sha256::new();
hasher.update(&password);
hasher.update(&hash.challenge);
hasher.finalize()[..].into()
let mut hasher2 = Sha256::new();
hasher2.update(&password[..]);
hasher2.update(&hash.challenge);
hasher2.finalize()[..].into()
};

let os_username = lc.read().unwrap().get_option("os-username");
Expand Down Expand Up @@ -3189,6 +3205,7 @@ pub async fn handle_login_from_ui(
hash_password = hasher2.finalize()[..].to_vec();

send_login(lc.clone(), os_username, os_password, hash_password, peer).await;
lc.write().unwrap().hash = lc.read().unwrap().hash.clone();
}

async fn send_switch_login_request(
Expand Down
Loading

0 comments on commit 9b9b847

Please sign in to comment.