Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
164 changes: 140 additions & 24 deletions lib/pages/clock_page.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'dart:async';
import 'dart:io' show Platform;
import 'dart:math' as math;

Expand All @@ -19,6 +20,10 @@ class ClockPage extends ConsumerStatefulWidget {
class _ClockPageState extends ConsumerState<ClockPage> {
bool _isMiniMode = false;
bool _isTogglingMiniMode = false;
bool _isLocked = false;
bool _isLocking = false;
bool _hudVisible = true;
Timer? _hudHideTimer;

@override
void didChangeDependencies() {
Expand All @@ -30,6 +35,7 @@ class _ClockPageState extends ConsumerState<ClockPage> {
void dispose() {
// 离开页面时恢复默认白色 tint(macOS/Windows 都支持更新 tint 颜色)
GlassTintController.instance.resetTintColor();
_hudHideTimer?.cancel();
super.dispose();
}

Expand Down Expand Up @@ -62,6 +68,13 @@ class _ClockPageState extends ConsumerState<ClockPage> {

final bool success;
if (_isMiniMode) {
// 退出mini mode时自动解锁
if (_isLocked) {
await controller.unlockWindow();
setState(() {
_isLocked = false;
});
}
success = await controller.exitPinnedMode();
} else {
success = await controller.enterPinnedMode();
Expand All @@ -79,6 +92,39 @@ class _ClockPageState extends ConsumerState<ClockPage> {
});
}

Future<void> _toggleLock() async {
if (_isLocking || !_isMiniMode) {
return;
}

final controller = WindowPinController.instance;
if (!controller.isSupported) {
return;
}

setState(() {
_isLocking = true;
});

final bool success;
if (_isLocked) {
success = await controller.unlockWindow();
} else {
success = await controller.lockWindow();
}

if (!mounted) {
return;
}

setState(() {
_isLocking = false;
if (success) {
_isLocked = !_isLocked;
}
});
}

void _handleBack(BuildContext context) {
if (Navigator.canPop(context)) {
context.pop();
Expand All @@ -87,6 +133,39 @@ class _ClockPageState extends ConsumerState<ClockPage> {
}
}

void _resetHudHideTimer() {
_hudHideTimer?.cancel();
_hudHideTimer = Timer(const Duration(seconds: 5), () {
if (mounted) {
setState(() {
_hudVisible = false;
});
}
});
}

void _handleMouseActivity() {
if (!_hudVisible && mounted) {
setState(() {
_hudVisible = true;
});
}
_resetHudHideTimer();
}

void _handleMouseEnter(PointerEvent event) {
_handleMouseActivity();
}

void _handleMouseHover(PointerEvent event) {
_handleMouseActivity();
}

void _handleMouseExit(PointerEvent event) {
// 鼠标离开窗口时开始计时隐藏
_resetHudHideTimer();
}

@override
Widget build(BuildContext context) {
final ref = this.ref;
Expand Down Expand Up @@ -133,49 +212,86 @@ class _ClockPageState extends ConsumerState<ClockPage> {

return Scaffold(
backgroundColor: useGlass ? Colors.transparent : clockBgColor,
body: SafeArea(
child: Padding(
padding: EdgeInsets.symmetric(horizontal: 32.w, vertical: 32.h),
body: MouseRegion(
onEnter: _handleMouseEnter,
onHover: _handleMouseHover,
onExit: _handleMouseExit,
opaque: false,
child: SafeArea(
child: Padding(
padding: EdgeInsets.symmetric(horizontal: 32.w, vertical: 32.h),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Row(
children: [
if (!_isMiniMode)
IconButton(
onPressed: () => _handleBack(context),
icon: Icon(
Icons.arrow_back_rounded,
color: theme.colorScheme.onPrimary,
Visibility(
visible: _hudVisible,
child: IconButton(
onPressed: () => _handleBack(context),
icon: Icon(
Icons.arrow_back_rounded,
color: theme.colorScheme.onPrimary,
),
tooltip: '返回仪表盘',
),
tooltip: '返回仪表盘',
),
if (_isMiniMode) SizedBox(width: 48.w, height: 48.w),
const Spacer(),
if (pinSupported)
IconButton(
iconSize: _isMiniMode ? 16 : 24,
padding: EdgeInsets.all(8.w),
constraints: BoxConstraints(
minWidth: 44.w,
minHeight: 44.w,
),
onPressed: _isTogglingMiniMode ? null : _toggleMiniMode,
icon: Icon(
_isMiniMode
? Icons.crop_square
: Icons.crop_square_outlined,
color: theme.colorScheme.onPrimary,
Visibility(
visible: _hudVisible,
child: IconButton(
iconSize: _isMiniMode ? 16 : 24,
padding: EdgeInsets.all(8.w),
constraints: BoxConstraints(
minWidth: 44.w,
minHeight: 44.w,
),
onPressed: _isTogglingMiniMode ? null : _toggleMiniMode,
icon: Icon(
_isMiniMode
? Icons.crop_square
: Icons.crop_square_outlined,
color: theme.colorScheme.onPrimary,
),
tooltip: _isMiniMode ? '退出迷你模式' : '进入迷你模式',
),
tooltip: _isMiniMode ? '退出迷你模式' : '进入迷你模式',
),
],
),
if (!_isMiniMode) SizedBox(height: 24.h),
Expanded(
child: _buildTimeContent(context, ref, theme, timeTextStyle),
),
],
// 在底部显示锁定按钮(仅在mini mode下)
if (pinSupported && _isMiniMode)
Align(
alignment: Alignment.bottomRight,
child: Padding(
padding: EdgeInsets.only(top: 8.h, right: 8.w),
child: Visibility(
visible: _hudVisible,
child: IconButton(
iconSize: 16,
padding: EdgeInsets.all(8.w),
constraints: BoxConstraints(
minWidth: 44.w,
minHeight: 44.w,
),
onPressed: _isLocking ? null : _toggleLock,
icon: Icon(
_isLocked ? Icons.lock_outline : Icons.lock_open,
color: theme.colorScheme.onPrimary,
),
tooltip: _isLocked ? '解锁窗口' : '锁定窗口',
),
),
),
),
],
),
),
),
),
Expand Down
Loading