Skip to content

Commit

Permalink
Merge pull request #157 from milikhin/harbour-settings
Browse files Browse the repository at this point in the history
Add Settings page to harbour-seabass
  • Loading branch information
milikhin authored Dec 11, 2022
2 parents 3d37ff3 + ad7d28f commit 97f1a7f
Show file tree
Hide file tree
Showing 20 changed files with 960 additions and 1,718 deletions.
4 changes: 0 additions & 4 deletions editor/__tests__/app/app.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,5 @@ describe('SeabassApp', () => {
// Check for 'appLoaded' action
const evt = await waitForApiMessage
expect(evt.detail.action).toEqual('appLoaded')
expect(evt.detail.data).toEqual({
isToolbarOpened: true,
directory: null
})
})
})
15 changes: 0 additions & 15 deletions editor/__tests__/app/model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ describe('SeabassAppModel', () => {
expect(model._viewport).toEqual({
verticalHtmlOffset: 0
})
expect(model.sailfishPreferences).toEqual({
isToolbarOpened: true,
directory: null
})
})
})

Expand Down Expand Up @@ -204,15 +200,4 @@ describe('SeabassAppModel', () => {
})
})
})

describe('#setSailfishPreferences', () => {
it('should set toolbar visibility', () => {
const model = new SeabassAppModel()

const options = { isToolbarOpened: true }
model.setSailfishPreferences(options)

expect(model._sailfish.isToolbarOpened).toEqual(options.isToolbarOpened)
})
})
})
3 changes: 1 addition & 2 deletions editor/src/api/api-interface.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { InputPreferences, SeabassSailfishPreferences, ViewportOptions } from '../app/model'
import { InputPreferences, ViewportOptions } from '../app/model'
import { KeyDownOptions } from '../editor/editor'

export interface ApiTransport {
Expand Down Expand Up @@ -56,7 +56,6 @@ export interface IncomingMessagePayload {
requestSaveAndClose: FileActionOptions
setContent: SetContentOptions
setPreferences: InputPreferences
setSailfishPreferences: SeabassSailfishPreferences
undo: undefined
toggleReadOnly: undefined
}
Expand Down
3 changes: 1 addition & 2 deletions editor/src/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import SocketApiTransport from './socket-transport'
/** Outgoing API message to a platform-specific app */
interface OutgoingApiMessage {
action: string
data: unknown
data?: unknown
}

interface ApiOptions {
Expand Down Expand Up @@ -45,7 +45,6 @@ export default class SeabassApi extends EventTarget {
'requestSaveAndClose',
'setContent',
'setPreferences',
'setSailfishPreferences',
'toggleReadOnly',
'undo',
'viewportChange'
Expand Down
16 changes: 3 additions & 13 deletions editor/src/app/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import SeabassView from './view'
import SeabassAppModel, {
EditorStateChangeOptions,
InputPreferences,
SeabassSailfishPreferences,
ViewportOptions
} from './model'

Expand Down Expand Up @@ -46,7 +45,7 @@ class SeabassApp {
this._tabs = new Tabs({ rootElem })

this._registerApiEventListeners()
this._api.send({ action: 'appLoaded', data: this._model.sailfishPreferences })
this._api.send({ action: 'appLoaded' })
}

/**
Expand All @@ -64,7 +63,6 @@ class SeabassApp {
this._api.addEventListener('requestSaveAndClose', this._onRequestFileSave.bind(this))
this._api.addEventListener('setContent', this._forwardEvent.bind(this))
this._api.addEventListener('setPreferences', this._onSetPreferences.bind(this))
this._api.addEventListener('setSailfishPreferences', this._onSetSailfishPreferences.bind(this))
this._api.addEventListener('toggleReadOnly', this._forwardEvent.bind(this))
this._api.addEventListener('undo', this._forwardEvent.bind(this))
this._api.addEventListener('viewportChange', this._onViewportChange.bind(this))
Expand Down Expand Up @@ -118,8 +116,8 @@ class SeabassApp {
}

/**
* Loads saved SailfishOS-specific preferences
* @param evt setTheme event
* Handles viewport changes
* @param evt viewportChange event
*/
_onViewportChange (evt: CustomEvent<ViewportOptions>): void {
this._model.setViewportOptions(evt.detail)
Expand All @@ -146,14 +144,6 @@ class SeabassApp {
this._model.setPreferences(evt.detail)
}

/**
* Loads saved SailfishOS-specific preferences
* @param evt setTheme event
*/
_onSetSailfishPreferences (evt: CustomEvent<SeabassSailfishPreferences>): void {
this._model.setSailfishPreferences(evt.detail)
}

/**
* Forwards UI state changes to the platform-specific app
* @param evt state change event
Expand Down
42 changes: 0 additions & 42 deletions editor/src/app/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,6 @@ export interface SeabassCommonPreferences {
useWrapMode?: boolean
}

export interface SeabassSailfishPreferences {
/** Bottom toolbar state */
isToolbarOpened: boolean
/** Current file tree directory */
directory: string|null
}

export interface ViewportOptions {
/** HTML page's offset from the bottom of webView. Used as a workaround to SfOS rendering issues */
verticalHtmlOffset: number
Expand All @@ -47,7 +40,6 @@ interface AppEvents {
loadFile: CustomEvent<FileLoadOptions>
log: CustomEvent<unknown>
preferencesChange: CustomEvent<SeabassCommonPreferences>
sfosPreferencesChange: CustomEvent<SeabassSailfishPreferences>
stateChange: CustomEvent<EditorStateChangeOptions>
}

Expand All @@ -72,36 +64,19 @@ export default class SeabassAppModel extends EventTarget {
useWrapMode: boolean
}

/** SailfishOS-specific preferences */
_sailfish: {
isToolbarOpened: boolean
directory: string|null
}

_viewport: {
verticalHtmlOffset: number
}

SFOS_TOOLBAR_LOCAL_STORAGE_KEY = 'sailfish__isToolbarOpened'
SFOS_DIRECTORY_LOCAL_STORAGE_KEY = 'sailfish__directory'

constructor () {
super()
this._editors = new Map()
this._preferences = { isDarkTheme: false, useWrapMode: true }
this._sailfish = {
isToolbarOpened: localStorage.getItem(this.SFOS_TOOLBAR_LOCAL_STORAGE_KEY) === 'true',
directory: localStorage.getItem(this.SFOS_DIRECTORY_LOCAL_STORAGE_KEY)
}
this._viewport = {
verticalHtmlOffset: 0
}
}

get sailfishPreferences (): SeabassSailfishPreferences {
return this._sailfish
}

addEventListener<T extends keyof AppEvents>(type: T,
callback: AppEventListener<T>, options?: EventListenerOptions): void {
super.addEventListener(type, callback as EventListenerOrEventListenerObject | null, options)
Expand Down Expand Up @@ -213,21 +188,4 @@ export default class SeabassAppModel extends EventTarget {
this.dispatchEvent(new CustomEvent('htmlThemeChange', { detail: this._htmlTheme }))
this.dispatchEvent(new CustomEvent('preferencesChange', { detail: this._preferences }))
}

/**
* Sets sailfish-specific app preferences
* @param options app preferences
*/
setSailfishPreferences (options: Partial<SeabassSailfishPreferences>): void {
if (options.isToolbarOpened !== undefined) {
this._sailfish.isToolbarOpened = options.isToolbarOpened
localStorage.setItem(this.SFOS_TOOLBAR_LOCAL_STORAGE_KEY, options.isToolbarOpened.toString())
}
if (options.directory !== undefined && options.directory !== null) {
this._sailfish.directory = options.directory
localStorage.setItem(this.SFOS_DIRECTORY_LOCAL_STORAGE_KEY, options.directory)
}

this.dispatchEvent(new CustomEvent('sfosPreferencesChange', { detail: this._sailfish }))
}
}
3 changes: 2 additions & 1 deletion editor/src/editor/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,10 @@ export default class Editor extends EventTarget {
* Destroys editor
*/
destroy (): void {
this._removeDomEventHandlers()
this._editor.destroy()
const editorParentElem = this._editorElem.parentElement as HTMLElement
editorParentElem.removeChild(this._editorElem)
this._removeDomEventHandlers()
}

dispatchEvent<T extends keyof Events> (event: Events[T]): boolean {
Expand Down
1 change: 1 addition & 0 deletions harbour-seabass/harbour-seabass.pro
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ DISTFILES += qml/harbour-seabass.qml \
qml/pages/Files.qml \
qml/pages/NewFile.qml \
qml/pages/SaveDialog.qml \
qml/pages/Settings.qml \
qml/py-backend \
rpm/harbour-seabass.changes.in \
rpm/harbour-seabass.changes.run.in \
Expand Down
10 changes: 6 additions & 4 deletions harbour-seabass/html/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

</style>
<style id="theme-css">
/* CSS Custom Properties are not supported in Sailfish */
/* CSS Custom Properties were not supported in Sailfish prior to v4.2 */
/* these values are going to be replaces within JS: */
body {
/*background-color: #backgroungColor */
Expand All @@ -18,15 +18,17 @@
#welcome a {
/* color: #highlightColor */
}
.cm-editor {
/* font-size: #fontSize */
}
</style>
</head>

<body>
<div id="welcome">
<p>
v0.10 introduces new file picker component.
It has compact design, remembers last opened directory and provides possiblity
to create new files.
v0.11 introduces Settings page.
It is possible to change font size and text wrapping.
</p>
<p>
Please let me know if you have any issues:
Expand Down
2 changes: 1 addition & 1 deletion harbour-seabass/qml/pages/About.qml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Page {
anchors.fill: parent
header: PageHeader {
id: header
title: qsTr('Seabass v%1').arg('0.10.0')
title: qsTr('Seabass v%1').arg('0.11.0')
}
model: ListModel {
ListElement {
Expand Down
76 changes: 58 additions & 18 deletions harbour-seabass/qml/pages/Editor.qml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Sailfish.Silica 1.0
import Sailfish.Pickers 1.0
import Sailfish.WebView 1.0
import Sailfish.WebEngine 1.0
import Nemo.Configuration 1.0

import '../generic/utils.js' as QmlJs
import '../components' as PlatformComponents
Expand Down Expand Up @@ -31,20 +32,43 @@ WebViewPage {
width: page.width
}

Component.onCompleted: {
pageStack.busyChanged.connect(function() {
if (!hasOpenedFile) {
return
}

if (!pageStack.busy) {
page.isMenuEnabled = false
}
})
}

ConfigurationValue {
id: configToolbarVisibility
key: "/apps/harbour-seabass/settings/is_toolbar_visible"
defaultValue: false
}

ConfigurationValue {
id: configFontSize
key: "/apps/harbour-seabass/settings/font_size"
defaultValue: 12
}

ConfigurationValue {
id: configUseWrapMode
key: "/apps/harbour-seabass/settings/soft_wrap_enabled"
defaultValue: true
}

GenericComponents.EditorState {
id: editorState
isDarkTheme: Theme.colorScheme === Theme.LightOnDark
directory: api.homeDir
fontSize: configFontSize.value
useWrapMode: configUseWrapMode.value
verticalHtmlOffset: headerHeight / WebEngineSettings.pixelRatio

onFilePathChanged: {
isMenuEnabled = false
}

onDirectoryChanged: {
api.postMessage('setSailfishPreferences', {
directory: directory
})
}
}

GenericComponents.EditorApi {
Expand All @@ -57,9 +81,6 @@ WebViewPage {

// API methods
onAppLoaded: function (data) {
toolbar.open = data.isToolbarOpened || false
// use `data.directory || api.homeDir` to restore last opened directory when opening app
editorState.directory = api.homeDir
editorState.loadTheme()
editorState.updateViewport()
}
Expand Down Expand Up @@ -87,7 +108,7 @@ WebViewPage {
page: page
title: hasOpenedFile
? ((editorState.hasChanges ? '*' : '') + QmlJs.getFileName(filePath))
: qsTr('Seabass v%1').arg('0.10.0')
: qsTr('Seabass v%1').arg('0.11.0')
description: hasOpenedFile
? QmlJs.getPrintableDirPath(QmlJs.getDirPath(filePath), api.homeDir)
: qsTr('Release notes')
Expand Down Expand Up @@ -169,6 +190,12 @@ WebViewPage {
toolbar.open = !toolbar.open
}
}
MenuItem {
text: qsTr('Settings')
onClicked: {
pageStack.push(settings)
}
}
MenuItem {
text: qsTr('About')
onClicked: {
Expand All @@ -183,7 +210,7 @@ WebViewPage {
width: parent.width
height: Theme.itemSizeMedium
focus: false
open: false
open: configToolbarVisibility.value
background: Rectangle {
// default background doesn't look good when virtual keyboard is opened
// hence the workaround with Rectangle
Expand All @@ -193,9 +220,7 @@ WebViewPage {
}

onOpenChanged: {
api.postMessage('setSailfishPreferences', {
isToolbarOpened: open
})
configToolbarVisibility.value = open
}

PlatformComponents.Toolbar {
Expand Down Expand Up @@ -290,6 +315,21 @@ WebViewPage {
}
}

Component {
id: settings
Settings {
fontSize: configFontSize.value
useWrapMode: configUseWrapMode.value

onFontSizeChanged: {
configFontSize.value = fontSize
}
onUseWrapModeChanged: {
configUseWrapMode.value = useWrapMode
}
}
}

/**
* Displays error message
* @param {string} [errorMessage] - error message to display
Expand Down
Loading

0 comments on commit 97f1a7f

Please sign in to comment.