Skip to content

Commit

Permalink
new window position algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
Merrit committed Aug 28, 2023
1 parent 881a6bf commit 1666af2
Show file tree
Hide file tree
Showing 8 changed files with 240 additions and 102 deletions.
30 changes: 27 additions & 3 deletions lib/app.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
Expand Down Expand Up @@ -36,12 +38,34 @@ class _AppState extends State<App> with TrayListener, WindowListener {

@override
void onWindowClose() {
final appWindow = context.read<AppWindow>();

if (settingsCubit.state.closeToTray) {
AppWindow.instance.hide();
return;
appWindow.hide();
} else {
super.onWindowClose();
appWindow.close();
}
}

Timer? timer;

@override
void onWindowEvent(String eventName) {
if (eventName == 'move' || eventName == 'resize') {
/// Set a timer between events that trigger saving the window size and
/// location. This is required because there is no notification available
/// for when these events *finish*, and therefore it would be triggered
/// hundreds of times otherwise during a move event.
timer?.cancel();
timer = null;
timer = Timer(
const Duration(seconds: 5),
() {
context.read<AppWindow>().saveWindowSizeAndPosition();
},
);
}
super.onWindowEvent(eventName);
}

@override
Expand Down
35 changes: 1 addition & 34 deletions lib/apps_list/apps_list_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import '../app/app.dart';
import '../core/core.dart';
import '../settings/cubit/settings_cubit.dart';
import '../theme/theme.dart';
import '../window/app_window.dart';
import 'apps_list.dart';

/// The main screen for Nyrna.
Expand All @@ -22,27 +21,7 @@ class AppsListPage extends StatefulWidget {
State<AppsListPage> createState() => _AppsListPageState();
}

class _AppsListPageState extends State<AppsListPage>
with WidgetsBindingObserver {
/// Tracks the current window size.
///
/// Updated in [initState], [dispose], and [didChangeMetrics] so that
/// we can save the window size when the user resizes the window.
late Size _appWindowSize;

@override
void initState() {
super.initState();
// Listen for changes to the application's window size.
WidgetsBinding.instance.addObserver(this);
}

@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}

class _AppsListPageState extends State<AppsListPage> {
@override
void didChangeDependencies() {
SchedulerBinding.instance.addPostFrameCallback((_) {
Expand All @@ -53,16 +32,6 @@ class _AppsListPageState extends State<AppsListPage>
super.didChangeDependencies();
}

@override
void didChangeMetrics() {
final updatedWindowSize = View.of(context).physicalSize;
if (_appWindowSize != updatedWindowSize) {
_appWindowSize = updatedWindowSize;
AppWindow.instance.saveWindowSize();
}
super.didChangeMetrics();
}

final ScrollController scrollController = ScrollController();

void _showFirstRunDialog() {
Expand All @@ -75,8 +44,6 @@ class _AppsListPageState extends State<AppsListPage>

@override
Widget build(BuildContext context) {
_appWindowSize = View.of(context).physicalSize;

return Scaffold(
appBar: const CustomAppBar(),
body: BlocBuilder<AppCubit, AppState>(
Expand Down
19 changes: 11 additions & 8 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import 'package:helpers/helpers.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:http/http.dart' as http;
import 'package:package_info_plus/package_info_plus.dart';
import 'package:window_manager/window_manager.dart';

import 'active_window/active_window.dart';
import 'app.dart';
Expand All @@ -28,7 +27,6 @@ import 'window/app_window.dart';

Future<void> main(List<String> args) async {
WidgetsFlutterBinding.ensureInitialized();
await windowManager.ensureInitialized();

// Parse command-line arguments.
final argParser = ArgumentParser() //
Expand Down Expand Up @@ -118,14 +116,19 @@ Future<void> main(List<String> args) async {
);

runApp(
MultiBlocProvider(
MultiRepositoryProvider(
providers: [
BlocProvider.value(value: appCubit),
BlocProvider.value(value: appsListCubit),
BlocProvider.value(value: settingsCubit),
BlocProvider.value(value: themeCubit),
RepositoryProvider.value(value: appWindow),
],
child: const App(),
child: MultiBlocProvider(
providers: [
BlocProvider.value(value: appCubit),
BlocProvider.value(value: appsListCubit),
BlocProvider.value(value: settingsCubit),
BlocProvider.value(value: themeCubit),
],
child: const App(),
),
),
);
}
4 changes: 0 additions & 4 deletions lib/settings/cubit/settings_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import '../../core/core.dart';
import '../../hotkey/hotkey_service.dart';
import '../../logs/logging_manager.dart';
import '../../storage/storage_repository.dart';
import '../../window/app_window.dart';

part 'settings_state.dart';
part 'settings_cubit.freezed.dart';
Expand All @@ -36,8 +35,6 @@ class SettingsCubit extends Cubit<SettingsState> {
for (final hotkey in state.appSpecificHotKeys) {
_hotkeyService.addHotkey(hotkey.hotkey);
}

AppWindow.instance.preventClose(state.closeToTray);
}

static Future<SettingsCubit> init({
Expand Down Expand Up @@ -134,7 +131,6 @@ class SettingsCubit extends Cubit<SettingsState> {
Future<void> updateCloseToTray([bool? closeToTray]) async {
if (closeToTray == null) return;

await AppWindow.instance.preventClose(closeToTray);
await _storage.saveValue(key: 'closeToTray', value: closeToTray);
emit(state.copyWith(closeToTray: closeToTray));
}
Expand Down
Loading

0 comments on commit 1666af2

Please sign in to comment.