Skip to content

Commit

Permalink
Refactoring. Added an offset to the window's setting, when it is chan…
Browse files Browse the repository at this point in the history
…ging position and dimension.
  • Loading branch information
ransome1 committed Jan 14, 2024
1 parent a668c94 commit 35b3e38
Show file tree
Hide file tree
Showing 27 changed files with 208 additions and 180 deletions.
2 changes: 1 addition & 1 deletion flatpak/com.github.ransome1.sleek.appdata.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<developer_name>Robin Ahle</developer_name>
<content_rating type="oars-1.1"/>
<releases>
<release version="2.0.7-rc.1" date="2024-01-13"/>
<release version="2.0.7-rc.2" date="2024-01-14"/>
</releases>
<url type="homepage">https://github.com/ransome1/sleek</url>
<url type="contact">https://github.com/ransome1/sleek/issues</url>
Expand Down
2 changes: 1 addition & 1 deletion flatpak/com.github.ransome1.sleek.desktop
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[Desktop Entry]
Version=2.0.7-rc.1
Version=2.0.7-rc.2
Name=sleek
Exec=sleek
Type=Application
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sleek",
"version": "2.0.7-rc.1",
"version": "2.0.7-rc.2",
"main": "./src/main/main.tsx",
"scripts": {
"build": "concurrently \"yarn run peggy\" \"yarn run build:main\" \"yarn run build:renderer\"",
Expand Down
2 changes: 1 addition & 1 deletion release/app/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sleek",
"version": "2.0.7-rc.1",
"version": "2.0.7-rc.2",
"description": "todo.txt manager for Linux, Windows and MacOS, free and open-source (FOSS)",
"synopsis": "todo.txt manager for Linux, Windows and MacOS, free and open-source (FOSS)",
"keywords": [
Expand Down
2 changes: 1 addition & 1 deletion snap/snapcraft.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: sleek
base: core20
version: "2.0.7-rc.1"
version: "2.0.7-rc.2"
summary: todo.txt manager for Linux, free and open-source (FOSS)
description: |
sleek is an open-source (FOSS) todo manager based on the todo.txt syntax. Stripped down to only the most necessary features, and with a clean and simple interface, sleek aims to help you focus on getting things done.
Expand Down
108 changes: 60 additions & 48 deletions src/main/config.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,17 @@ if(!fs.existsSync(userDataDirectory)) fs.mkdirSync(userDataDirectory)
console.log('config.ts: sleek userdata is located at: ' + userDataDirectory);

const customStylesPath: string = path.join(userDataDirectory, 'customStyles.css');
if(!fs.existsSync(customStylesPath)) {
fs.writeFileSync(customStylesPath, '');
}

const configStorage: Store<Settings> = new Store<Settings>({
const config: Store<Settings> = new Store<Settings>({
cwd: userDataDirectory,
name: 'config',
beforeEachMigration: context => {
console.log(`[config.json] migrating from ${context.fromVersion}${context.toVersion}`);
},
migrations: {
'2.0.0': store => {
store.setConfig('sorting', [
console.log('Creating new default configuration for v2.0.0');
store.set('sorting', [
{ id: '1', value: 'priority', invert: false },
{ id: '2', value: 'projects', invert: false },
{ id: '3', value: 'contexts', invert: false },
Expand All @@ -42,7 +43,7 @@ const configStorage: Store<Settings> = new Store<Settings>({
{ id: '8', value: 'rec', invert: false },
{ id: '9', value: 'pm', invert: false },
]);
store.setConfig('accordionOpenState', [
store.set('accordionOpenState', [
true,
true,
true,
Expand All @@ -53,49 +54,52 @@ const configStorage: Store<Settings> = new Store<Settings>({
false,
false
]);
store.setConfig('files', []);
store.setConfig('appendCreationDate', false);
store.setConfig('showCompleted', true);
store.setConfig('showHidden', false);
store.setConfig('windowMaximized', false);
store.setConfig('fileSorting', false);
store.setConfig('convertRelativeToAbsoluteDates', true);
store.setConfig('thresholdDateInTheFuture', true);
store.setConfig('colorTheme', 'system');
store.setConfig('shouldUseDarkColors', false);
store.setConfig('notificationsAllowed', true);
store.setConfig('notificationThreshold', 2);
store.setConfig('showFileTabs', true);
store.setConfig('isNavigationOpen', true);
store.setConfig('customStylesPath', customStylesPath);
store.setConfig('tray', false);
store.setConfig('zoom', 100);
store.setConfig('multilineTextField', false);
store.setConfig('useMultilineForBulkTodoCreation', false);
store.setConfig('matomo', true);
store.set('files', []);
store.set('appendCreationDate', false);
store.set('showCompleted', true);
store.set('showHidden', false);
store.set('windowMaximized', false);
store.set('fileSorting', false);
store.set('convertRelativeToAbsoluteDates', true);
store.set('thresholdDateInTheFuture', true);
store.set('colorTheme', 'system');
store.set('shouldUseDarkColors', false);
store.set('notificationsAllowed', true);
store.set('notificationThreshold', 2);
store.set('showFileTabs', true);
store.set('isNavigationOpen', true);
store.set('customStylesPath', customStylesPath);
store.set('tray', false);
store.set('zoom', 100);
store.set('multilineTextField', false);
store.set('useMultilineForBulkTodoCreation', false);
store.set('matomo', true);
},
'2.0.1': store => {
store.setConfig('anonymousUserId', anonymousUserId);
console.log('Migrating from 2.0.0 → 2.0.1');
store.set('anonymousUserId', anonymousUserId);
},
'2.0.2': store => {
store.setConfig('dueDateInTheFuture', true);
console.log('Migrating from 2.0.1 → 2.0.2');
store.set('dueDateInTheFuture', true);
},
'2.0.4': store => {
console.log('Migrating from 2.0.2 → 2.0.4');
store.delete('multilineTextField');
store.delete('isDrawerOpen');
store.delete('useMultilineForBulkTodoCreation');
store.setConfig('bulkTodoCreation', false);
store.setConfig('disableAnimations', false);
store.set('bulkTodoCreation', false);
store.set('disableAnimations', false);
},
}
});

const filterStorage = new Store<Filters>({ cwd: userDataDirectory, name: 'filters' });
const filter = new Store<Filters>({ cwd: userDataDirectory, name: 'filters' });

if(!filterStorage.has('search')) {
filterStorage.set('search', []);
} else if(!filterStorage.has('attributes')) {
filterStorage.set('attributes', {});
if(!filter.has('search')) {
filter.set('search', []);
} else if(!filter.has('attributes')) {
filter.set('attributes', {});
}

const notifiedTodoObjectsPath = path.join(userDataDirectory, 'notifiedTodoObjects.json');
Expand All @@ -106,19 +110,15 @@ if(!fs.existsSync(notifiedTodoObjectsPath)) {
fs.writeFileSync(notifiedTodoObjectsPath, JSON.stringify(defaultNotifiedTodoObjectsData));
}

if(!fs.existsSync(customStylesPath)) {
fs.writeFileSync(customStylesPath, '');
}

filterStorage.onDidAnyChange(async () => {
filter.onDidChange('attributes', async () => {
try {
await processDataRequest(searchString);
} catch(error: any) {
console.error(error);
}
});

configStorage.onDidAnyChange(async(settings) => {
config.onDidAnyChange(async(settings) => {
try {
await processDataRequest(searchString);
mainWindow!.webContents.send('settingsChanged', settings);
Expand All @@ -127,23 +127,35 @@ configStorage.onDidAnyChange(async(settings) => {
}
});

configStorage.onDidChange('files', (files: FileObject[] | null) => {
config.onDidChange('files', (newValue: FileObject[] | null) => {
try {
if(files) {
createFileWatcher(files);
if (newValue) {
createFileWatcher(newValue);
}
} catch(error: any) {
} catch (error: any) {
console.error(error);
}
});

configStorage.onDidChange('colorTheme', (colorTheme) => {
config.onDidChange('windowPosition', (windowPosition) => {
console.log(windowPosition);
});

config.onDidChange('windowDimensions', (windowDimensions) => {
console.log(windowDimensions);
});

config.onDidChange('windowMaximized', (windowMaximized) => {
console.log(windowMaximized);
});

config.onDidChange('colorTheme', (colorTheme) => {
if(colorTheme === 'system' || colorTheme === 'light' || colorTheme === 'dark') {
nativeTheme.themeSource = colorTheme;
}
});

configStorage.onDidChange('tray', () => {
config.onDidChange('tray', () => {
try {
createTray();
} catch(error: any) {
Expand All @@ -153,4 +165,4 @@ configStorage.onDidChange('tray', () => {

nativeTheme.on('updated', handleTheme);

export { configStorage, filterStorage, notifiedTodoObjectsStorage };
export { config, filter, notifiedTodoObjectsStorage };
60 changes: 37 additions & 23 deletions src/main/main.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import { app, BrowserWindow, nativeTheme } from 'electron';
import path from 'path';
import fs from 'fs';
import { configStorage } from './config';
import { config } from './config';
import { createMenu } from './modules/Menu';
import { getAssetPath, resolveHtmlPath } from './util';
import { createFileWatcher, watcher } from './modules/File/Watcher';
import { addFile } from './modules/File/File';
import { createTray } from './modules/Tray';
import debounce from 'lodash/debounce';
import './modules/IpcMain';

const environment: string | undefined = process.env.NODE_ENV;
let mainWindow: BrowserWindow | null = null;
let eventListeners: Record<string, any | undefined> = {};
let resizeTimeout;

const handleCreateWindow = () => {
if(mainWindow) {
Expand All @@ -37,48 +39,60 @@ const handleClosed = async () => {
eventListeners.watcher = undefined;
}

const handleResize = () => {
const rectangle = mainWindow?.getBounds() as WindowRectangle;
const width = rectangle.width;
const height = rectangle.height;
configStorage.set('windowDimensions', { width, height });
configStorage.set('windowMaximized', false);
const handleResize = (event) => {
clearTimeout(resizeTimeout);
resizeTimeout = setTimeout(() => {
const rectangle = mainWindow?.getBounds() as WindowRectangle;
const width = rectangle.width;
const height = rectangle.height;
config.set('windowDimensions', { width, height });
config.set('windowMaximized', false);
}, 500);
}

const handleMove = () => {
const rectangle = mainWindow?.getBounds() as WindowRectangle;
const x = rectangle.x;
const y = rectangle.y;
configStorage.set('windowPosition', { x, y });
configStorage.set('windowMaximized', false);
clearTimeout(resizeTimeout);
resizeTimeout = setTimeout(() => {
const rectangle = mainWindow?.getBounds() as WindowRectangle;
const x = rectangle.x;
const y = rectangle.y;
config.set('windowPosition', { x, y });
config.set('windowMaximized', false);
}, 500);
}

const handleUnmaximize = () => {
configStorage.set('windowMaximized', false);
clearTimeout(resizeTimeout);
resizeTimeout = setTimeout(() => {
config.set('windowMaximized', false);
}, 500);
}

const handleMaximize = () => {
configStorage.set('windowMaximized', true)
clearTimeout(resizeTimeout);
resizeTimeout = setTimeout(() => {
config.set('windowMaximized', true)
}, 500);
}

const handleShow = () => {
app.dock?.show();
}

const handleWindowSizeAndPosition = () => {
const isMaximized = configStorage.get('windowMaximized');
const isMaximized = config.get('windowMaximized');
if(isMaximized) {
mainWindow?.maximize();
return;
}

const windowDimensions: { width: number; height: number } | null = configStorage.get('windowDimensions') as { width: number; height: number } | null;
const windowDimensions: { width: number; height: number } | null = config.get('windowDimensions') as { width: number; height: number } | null;

if(windowDimensions) {
const { width, height } = windowDimensions;
mainWindow?.setSize(width, height);

const windowPosition: { x: number; y: number } | null = configStorage.get('windowPosition') as { x: number; y: number } | null;
const windowPosition: { x: number; y: number } | null = config.get('windowPosition') as { x: number; y: number } | null;
if(windowPosition) {
const { x, y } = windowPosition;
mainWindow?.setPosition(x, y);
Expand All @@ -87,7 +101,7 @@ const handleWindowSizeAndPosition = () => {
}

const createMainWindow = () => {
const shouldUseDarkColors: boolean = configStorage.get('shouldUseDarkColors');
const shouldUseDarkColors: boolean = config.get('shouldUseDarkColors');
mainWindow = new BrowserWindow({
width: 1280,
height: 1000,
Expand All @@ -106,15 +120,15 @@ const createMainWindow = () => {

mainWindow?.loadURL(resolveHtmlPath('index.html'));

const files: FileObject[] = (configStorage.get('files') as FileObject[]) || [];
const files: FileObject[] = (config.get('files') as FileObject[]) || [];
if(files) {
createFileWatcher(files);
}
createMenu(files);

handleWindowSizeAndPosition();

const colorTheme = configStorage.get('colorTheme');
const colorTheme = config.get('colorTheme');
nativeTheme.themeSource = colorTheme;

mainWindow
Expand All @@ -132,7 +146,7 @@ const createMainWindow = () => {
eventListeners.handleMaximize = handleMaximize
eventListeners.handleUnmaximize = handleUnmaximize;

const tray: boolean = configStorage.get('tray');
const tray: boolean = config.get('tray');
if(tray) {
createTray();
}
Expand All @@ -141,7 +155,7 @@ const createMainWindow = () => {
mainWindow.webContents.openDevTools();
}

const customStylesPath: string = configStorage.get('customStylesPath');
const customStylesPath: string = config.get('customStylesPath');
if(customStylesPath) {
fs.readFile(customStylesPath, 'utf8', (error: Error | null, data) => {
if(!error) {
Expand All @@ -155,7 +169,7 @@ const createMainWindow = () => {
}

const handleWindowAllClosed = () => {
const tray = configStorage.get('tray');
const tray = config.get('tray');
if(process.platform !== 'darwin' && !tray) {
app.quit();
} else if(process.platform === 'darwin' && tray) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/modules/Date.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import Sugar from 'sugar';
import dayjs from 'dayjs';
import { configStorage } from '../config';
import { config } from '../config';

function mustNotify(date: Date): boolean {
const today = dayjs().startOf('day');
const notificationThreshold: number = configStorage.get('notificationThreshold');
const notificationThreshold: number = config.get('notificationThreshold');
return dayjs(date).isBefore(today.add(notificationThreshold, 'day')) || false;
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/modules/File/Active.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { configStorage } from '../../config';
import { config } from '../../config';

export function getActiveFile(): FileObject | null {
const files: FileObject[] = configStorage.get('files');
const files: FileObject[] = config.get('files');
if(files.length === 0) return null;
const activeIndex = files.findIndex((file) => file.active);
return files[activeIndex];
Expand Down
Loading

0 comments on commit 35b3e38

Please sign in to comment.