Skip to content

Commit

Permalink
⚡ Support for Multi-monitor setups.
Browse files Browse the repository at this point in the history
- This fixes #1.
  • Loading branch information
omegaui committed Jan 8, 2024
1 parent cbc0eda commit c47ee5d
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 7 deletions.
2 changes: 1 addition & 1 deletion AppImageBuilder.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ AppDir:
id: org.omegaui.cliptopia
name: Cliptopia
icon: app-icon
version: 1.0.0+4
version: 1.0.0+5
exec: cliptopia
exec_args: $@
apt:
Expand Down
2 changes: 1 addition & 1 deletion debian/debian.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ flutter_app:

control:
Package: Cliptopia
Version: 1.0.0+4
Version: 1.0.0+5
Architecture: amd64
Essential: no
Priority: optional
Expand Down
74 changes: 74 additions & 0 deletions lib/app/powermode/presentation/dialogs/power_mode_settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import 'package:cliptopia/config/assets/app_artworks.dart';
import 'package:cliptopia/config/assets/app_icons.dart';
import 'package:cliptopia/config/themes/app_theme.dart';
import 'package:cliptopia/constants/meta_info.dart';
import 'package:cliptopia/core/argument_handler.dart';
import 'package:cliptopia/core/storage/storage.dart';
import 'package:cliptopia/core/utils.dart';
import 'package:flutter/material.dart';
import 'package:gap/gap.dart';
import 'package:url_launcher/url_launcher_string.dart';
Expand Down Expand Up @@ -227,6 +229,78 @@ class _PowerModeSettingsState extends State<PowerModeSettings> {
),
),
const Gap(25),
if (isMultiMonitorSetup()) ...[
Padding(
padding: const EdgeInsets.only(
left: 36,
right: 36,
),
child: SizedBox(
height: 50,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Image.asset(
AppIcons.desktop,
width: 48,
height: 48,
fit: BoxFit.fitWidth,
),
const SizedBox(width: 11),
Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
"Choose the target monitor${ArgumentHandler.isDebugMode() ? " (In Test Mode)" : ""}",
style: AppTheme.fontSize(16).makeBold(),
),
const SizedBox(height: 5),
Text(
"Select the monitor on which the power mode window should appear",
style: AppTheme.fontSize(14),
),
],
),
Expanded(
child: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
SizedBox(
width: 40,
child: DropdownButton<int>(
items: List<int>.generate(getMonitorCount(),
(index) => index + 1)
.map(
(e) => DropdownMenuItem(
value: e,
child: Text(
"$e",
style: AppTheme.fontSize(14)
.makeBold(),
),
),
)
.toList(),
value:
Storage.getSelectedMonitorIndex() + 1,
onChanged: (int? value) {
setState(() {
Storage.setSelectedMonitorIndex(
(value ?? 1) - 1);
});
},
),
),
],
),
),
],
),
),
),
],
const Gap(25),
Option(
title: "Keep the images hidden until triggered",
description:
Expand Down
2 changes: 1 addition & 1 deletion lib/constants/meta_info.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'package:cliptopia/core/utils.dart';
class MetaInfo {
MetaInfo._();

static String get version => "v1.0.0+4-beta";
static String get version => "v1.0.0+5-beta";
static String get daemonCompatibleVersion => "v0.0.3";
static const daemonDownloadUrl =
"https://github.com/omegaui/cliptopia_daemon/raw/master/cliptopia-daemon";
Expand Down
13 changes: 13 additions & 0 deletions lib/core/storage/storage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import 'package:flutter/services.dart';
class Storage {
static late final File copyScript;
static late final File commandExecutorScript;
static final File monitorIndexFile =
File(combineHomePath(['.config', 'cliptopia', 'monitor.index']));
static late final JsonConfigurator _storage;

Storage._();
Expand Down Expand Up @@ -74,6 +76,17 @@ class Storage {
static bool isSensitivityOn() {
return get(StorageKeys.sensitivity, fallback: false);
}

static int getSelectedMonitorIndex() {
if (!monitorIndexFile.existsSync()) {
return 0;
}
return int.tryParse(monitorIndexFile.readAsStringSync()) ?? 0;
}

static void setSelectedMonitorIndex(int index) {
monitorIndexFile.writeAsStringSync("$index");
}
}

class TempStorage {
Expand Down
11 changes: 11 additions & 0 deletions lib/core/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@ String _pgrep(pattern) {
return Process.runSync('pgrep', ['-x', pattern]).stdout;
}

int getMonitorCount() {
if (ArgumentHandler.isDebugMode()) {
return 3; // for enabling the full settings interface
}
return WidgetsBinding.instance.platformDispatcher.views.length;
}

bool isMultiMonitorSetup() {
return getMonitorCount() > 1;
}

bool isDaemonAlive() {
final daemonProcess = _pgrep('cliptopia-daemon');
final devProcess = _pgrep('dart:cliptopia_');
Expand Down
45 changes: 42 additions & 3 deletions linux/my_application.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
#include "my_application.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <bitsdojo_window_linux/bitsdojo_window_plugin.h>

#include <flutter_linux/flutter_linux.h>
Expand All @@ -12,6 +16,39 @@

static gchar** global_argv = NULL;


int readMonitorIndex() {
// Get the user's home directory
const char *homeDir = getenv("HOME");
if (homeDir == NULL) {
fprintf(stderr, "Error: HOME environment variable not set.\n");
return 0;
}

// Create the full path to the monitor configuration file
char configFile[256];
snprintf(configFile, sizeof(configFile), "%s/.config/cliptopia/monitor.index", homeDir);

// Open the file for reading
FILE *file = fopen(configFile, "r");
if (file == NULL) {
fprintf(stderr, "Error opening file: %s\n", configFile);
return 0;
}

// Read the integer from the file
int monitorIndex = 0;
if (fscanf(file, "%d", &monitorIndex) != 1) {
fprintf(stderr, "Error reading monitor index from file: %s\n", configFile);
fclose(file);
}

// Close the file
fclose(file);

return monitorIndex;
}

bool hasFlag(const char* flag, int argc, char** argv) {
for (int i = 1; i < argc; ++i) {
if (argv[i] != NULL && g_strcmp0(argv[i], flag) == 0) {
Expand Down Expand Up @@ -66,11 +103,13 @@ static void my_application_activate(GApplication* application) {
gtk_window_set_resizable(window, FALSE);

if (global_argv != NULL &&
hasFlag("--silent", length, global_argv) &&
hasFlag("--power", length, global_argv)) {
hasFlag("--silent", length, global_argv) &&
hasFlag("--power", length, global_argv)) {
// Read monitor index from the monitor config file
int monitorIndex = readMonitorIndex();
GdkScreen* screen = gtk_window_get_screen(window);
GdkRectangle monitor_rect;
gdk_screen_get_monitor_geometry(screen, 0, &monitor_rect);
gdk_screen_get_monitor_geometry(screen, monitorIndex, &monitor_rect);
gtk_window_set_default_size(window, monitor_rect.width, monitor_rect.height);
} else {
gtk_window_set_default_size(window, 750, 650);
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description: The state-of-the-art clipboard manager.

publish_to: 'none' # Remove this line if you wish to publish to pub.dev

version: 1.0.0+4
version: 1.0.0+5

environment:
sdk: '>=3.1.4 <4.0.0'
Expand Down

0 comments on commit c47ee5d

Please sign in to comment.