Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added hide to tray option #6915

Open
wants to merge 4 commits into
base: development
Choose a base branch
from
Open
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
23,182 changes: 23,182 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

12 changes: 11 additions & 1 deletion src/datastores/handlers/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ class Settings {
{ _id: 'useProxy' },
{ _id: 'proxyProtocol' },
{ _id: 'proxyHostname' },
{ _id: 'proxyPort' }
{ _id: 'proxyPort' },
{ _id: 'hideToTrayOnClose' },
{ _id: 'hideToTrayOnMinimize' }
]
})
}
Expand All @@ -73,6 +75,14 @@ class Settings {
}
}

static _findHideToTrayOnClose() {
return db.settings.findOneAsync({ _id: 'hideToTrayOnClose' })
}

static _findHideToTrayOnMinimize() {
return db.settings.findOneAsync({ _id: 'hideToTrayOnMinimize' })
}

static _updateBounds(value) {
return db.settings.updateAsync({ _id: 'bounds' }, { _id: 'bounds', value }, { upsert: true })
}
Expand Down
99 changes: 89 additions & 10 deletions src/main/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {
app, BrowserWindow, dialog, Menu, ipcMain,
powerSaveBlocker, screen, session, shell,
nativeTheme, net, protocol, clipboard
nativeTheme, net, protocol, clipboard,
Tray, nativeImage
} from 'electron'
import path from 'path'
import cp from 'child_process'
Expand All @@ -16,7 +17,7 @@ import {
} from '../constants'
import * as baseHandlers from '../datastores/handlers/base'
import { extractExpiryTimestamp, ImageCache } from './ImageCache'
import { existsSync } from 'fs'
import { existsSync, readFileSync } from 'fs'
import asyncFs from 'fs/promises'
import { promisify } from 'util'
import { brotliDecompress } from 'zlib'
Expand Down Expand Up @@ -231,6 +232,9 @@ function runApp() {

let mainWindow
let startupUrl
let tray = null
let trayOnClose
let trayOnMinimize

const userDataPath = app.getPath('userData')

Expand Down Expand Up @@ -391,6 +395,12 @@ function runApp() {
case 'proxyPort':
proxyPort = doc.value
break
case 'hideToTrayOnClose':
trayOnClose = doc.value
break
case 'hideToTrayOnMinimize':
trayOnMinimize = doc.value
break
}
})
}
Expand Down Expand Up @@ -593,6 +603,38 @@ function runApp() {
}
})

function createTray() {
const icon = process.env.NODE_ENV === 'development'
? readFileSync(path.join(__dirname, '..', '..', '_icons', 'iconColor.png'))
: readFileSync(path.join(path.dirname(__dirname), '_icons', 'iconColor.png'))

tray = new Tray(nativeImage.createFromBuffer(icon))
tray.setIgnoreDoubleClickEvents(true)

function click() {
mainWindow.show()
tray.destroy()
}

const menu = Menu.buildFromTemplate([
{
label: 'Show',
click: () => click()
},
{
label: 'Quit',
click: handleQuit
}
])

tray.setContextMenu(menu)
tray.setToolTip('FreeTube')

tray.on('click', (event) => {
click()
})
}

/**
* @param {string} extension
*/
Expand Down Expand Up @@ -749,6 +791,26 @@ function runApp() {

// endregion Ensure child windows use same options since electron 14

newWindow.on('close', (event) => {
if (trayOnClose) {
event.preventDefault()
newWindow.hide()
createTray()
}
})

newWindow.on('minimize', (event) => {
if (trayOnMinimize) {
event.preventDefault()
newWindow.hide()
createTray()
}
})

if (tray) {
tray.destroy()
}

if (replaceMainWindow) {
mainWindow = newWindow
}
Expand Down Expand Up @@ -1245,6 +1307,13 @@ function runApp() {
await setMenu()
break

case 'hideToTrayOnClose':
trayOnClose = data.value
break
case 'hideToTrayOnMinimize':
trayOnMinimize = data.value
break

default:
// Do nothing for unmatched settings
}
Expand Down Expand Up @@ -1643,14 +1712,11 @@ function runApp() {

let resourcesCleanUpDone = false

app.on('window-all-closed', () => {
app.on('window-all-closed', async () => {
if (trayOnClose) { return }

// Clean up resources (datastores' compaction + Electron cache and storage data clearing)
cleanUpResources().finally(() => {
mainWindow = null
if (process.platform !== 'darwin') {
app.quit()
}
})
handleQuit()
})

if (process.platform === 'darwin') {
Expand All @@ -1661,7 +1727,7 @@ function runApp() {
app.on('will-quit', e => {
// Let app quit when the cleanup is finished

if (resourcesCleanUpDone) { return }
if (resourcesCleanUpDone || trayOnClose) { return }

e.preventDefault()
cleanUpResources().finally(() => {
Expand All @@ -1673,6 +1739,19 @@ function runApp() {
})
}

app.on('before-quit', () => {
tray.destroy()
})

function handleQuit() {
cleanUpResources().finally(() => {
mainWindow.destroy()
if (process.platform !== 'darwin') {
app.quit()
}
})
}

async function cleanUpResources() {
if (resourcesCleanUpDone) {
return
Expand Down
10 changes: 10 additions & 0 deletions src/renderer/components/general-settings/general-settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,14 @@ export default defineComponent({

openDeepLinksInNewWindow: function () {
return this.$store.getters.getOpenDeepLinksInNewWindow
},

hideToTrayOnClose: function () {
return this.$store.getters.getHideToTrayOnClose
},

hideToTrayOnMinimize: function () {
return this.$store.getters.getHideToTrayOnMinimize
}
},
created: function () {
Expand Down Expand Up @@ -273,6 +281,8 @@ export default defineComponent({
'updateExternalLinkHandling',
'updateGeneralAutoLoadMorePaginatedItemsEnabled',
'updateOpenDeepLinksInNewWindow',
'updateHideToTrayOnClose',
'updateHideToTrayOnMinimize',
])
}
})
12 changes: 12 additions & 0 deletions src/renderer/components/general-settings/general-settings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@
:tooltip="$t('Settings.General Settings.Auto Load Next Page.Tooltip')"
@change="updateGeneralAutoLoadMorePaginatedItemsEnabled"
/>
<ft-toggle-switch
:label="$t('Settings.General Settings.Hide to tray on close')"
:default-value="hideToTrayOnClose"
:compact="true"
@change="updateHideToTrayOnClose"
/>
</div>
<div class="switchColumn">
<ft-toggle-switch
Expand All @@ -45,6 +51,12 @@
:tooltip="$t('Tooltips.General Settings.Open Deep Links In New Window')"
@change="updateOpenDeepLinksInNewWindow"
/>
<ft-toggle-switch
:label="$t('Settings.General Settings.Hide to tray on minimize')"
:default-value="hideToTrayOnMinimize"
:compact="true"
@change="updateHideToTrayOnMinimize"
/>
</div>
</div>
<div class="switchGrid">
Expand Down
2 changes: 2 additions & 0 deletions src/renderer/store/modules/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,8 @@ const state = {
// If the playlist is removed quick bookmark is disabled
quickBookmarkTargetPlaylistId: 'favorites',
generalAutoLoadMorePaginatedItemsEnabled: false,
hideToTrayOnClose: false,
hideToTrayOnMinimize: false,

// The settings below have side effects
currentLocale: 'system',
Expand Down
2 changes: 2 additions & 0 deletions static/locales/en-US.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,8 @@ Settings:
on Failure
Enable Search Suggestions: Enable Search Suggestions
Open Deep Links In New Window: Open URLs Passed to FreeTube in a New Window
Hide to tray on close: Hide to tray on close
Hide to tray on minimize: Hide to tray on minimize
Auto Load Next Page:
Label: Auto Load Next Page
Tooltip: Load additional pages and comments automatically.
Expand Down
2 changes: 2 additions & 0 deletions static/locales/es.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,8 @@ Settings:
Label: Carga automática de la página siguiente
Tooltip: Cargar automáticamente páginas adicionales y comentarios.
Open Deep Links In New Window: Abrir URL pasadas a FreeTube en una nueva ventana
Hide to tray on close: Ocultar a la bandeja del sistema al cerrar
Hide to tray on minimize: Ocultar a la bandeja del sistema al minimizar
Theme Settings:
Theme Settings: 'Tema'
Match Top Bar with Main Color: 'Usar color principal para barra superior'
Expand Down
Loading
Loading