/
tab-size.js
180 lines (171 loc) · 7.65 KB
/
tab-size.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// Change the width of a tab
(function(){
const defaultSettings = {
horizontal: {
unpinnedActiveWidthMax: 180,
unpinnedInactiveWidthMax: 180,
pinnedActiveWidthMax: 30,
pinnedInactiveWidthMax: 30,
pinnedAllowShrink: false
},
vertical: {
unpinnedActiveHeightMax: 150,
unpinnedInactiveHeightMax: 150,
pinnedActiveHeightMax: 30,
pinnedInactiveHeightMax: 30,
unpinnedActiveHeightMin: 30,
unpinnedInactiveHeightMin: 30,
pinnedActiveHeightMin: 30,
pinnedInactiveHeightMin: 30,
}
}
const maxWidthLimit = 500
const maxHeightLimit = 200
function completeObject(obj, fallback = defaultSettings) {
Object.keys(fallback).forEach(key => {
if (!obj.hasOwnProperty(key))
obj[key] = fallback[key]
else if (typeof obj[key] == "object")
obj[key] = completeObject(obj[key], fallback[key])
})
return obj
}
vivaldi.jdhooks.hookModuleExport("vivaldiSettings", "default", exports => {
let oldGetDefault = exports.getDefault
exports.getDefault = name => {
switch (name) {
case "TAB_SIZE": return defaultSettings
default: return oldGetDefault(name)
}
}
return exports
})
vivaldi.jdhooks.hookClass('tabs_TabStrip', cls => {
return vivaldi.jdhooks.insertWatcher(class extends cls {
getTabStyle(info, horz, focus, pin, stack, thumb) {
let tabStyle = super.getTabStyle(...arguments)
const tabSize = completeObject(this.state.jdVivaldiSettings.TAB_SIZE)
if (horz) {
const key = (pin ? 'pinned' : 'unpinned')
+ (focus ? 'Active' : 'Inactive') + 'WidthMax'
tabStyle.maxWidth = tabSize.horizontal[key]
if (pin) {
if (tabSize.horizontal.pinnedAllowShrink) {
tabStyle.flex = true
delete tabStyle.minWidth
} else tabStyle.minWidth = tabStyle.maxWidth
}
if (tabStyle.maxWidth > maxWidthLimit)
delete tabStyle.maxWidth
} else {
const key = (pin ? 'pinned' : 'unpinned')
+ (focus ? 'Active' : 'Inactive')
tabStyle.maxHeight = tabSize.vertical[key + 'HeightMax']
tabStyle.minHeight = tabSize.vertical[key + 'HeightMin']
if (tabStyle.maxHeight < tabStyle.minHeight)
tabStyle.maxHeight = tabStyle.minHeight
if (tabStyle.maxHeight > maxHeightLimit)
delete tabStyle.maxHeight
tabStyle.flex = !tabStyle.maxHeight || (tabStyle.minHeight != tabStyle.maxHeight)
}
return tabStyle
}
}, { settings: ['TAB_SIZE'] })
})
// Settings
vivaldi.jdhooks.hookClass('settings_tabs_TabOptions', cls => {
const React = vivaldi.jdhooks.require('React')
const settings = vivaldi.jdhooks.require('vivaldiSettings')
const PrefKeys = vivaldi.jdhooks.require("_PrefKeys")
const TabWidthSettings = vivaldi.jdhooks.insertWatcher(class extends React.Component {
onSliderChanged(dir, setting, event) {
if (event.target && event.target.value) {
let newState = completeObject(this.state.jdVivaldiSettings.TAB_SIZE)
let newVal = parseInt([event.target.value])
newState[dir] = {
...newState[dir],
...{ [setting]: newVal }
}
settings.set({ ['TAB_SIZE']: newState })
}
}
onAllowShrinkChanged(event) {
if (event.target && event.target.checked !== null) {
let newState = completeObject(this.state.jdVivaldiSettings.TAB_SIZE)
newState.horizontal = {
...newState.horizontal,
...{ ['pinnedAllowShrink']: !!event.target.checked }
}
settings.set({ ['TAB_SIZE']: newState })
}
}
createSlider(dir, setting, label, canAuto = true) {
let val = completeObject(this.state.jdVivaldiSettings.TAB_SIZE)[dir][setting]
let displ = ''
const maxLimit = (dir == 'horizontal') ? maxWidthLimit : maxHeightLimit
if (canAuto) {
if (val > maxLimit) val = maxLimit + 10
displ = val > maxLimit ? 'Auto' : val + 'px'
} else {
val = Math.min(val, maxLimit)
displ = val + 'px'
}
return React.createElement('div', { className: 'setting-single' },
React.createElement('h3', null, label),
React.createElement('input', {
type: 'range',
value: val,
min: 30, // Less than 30 seems to have no effect
max: canAuto ? maxLimit + 10 : maxLimit,
step: 10,
onChange: this.onSliderChanged.bind(this, dir, setting)
}),
React.createElement('span', null, displ))
}
render() {
const tabSize = completeObject(this.state.jdVivaldiSettings.TAB_SIZE)
const barPosition = this.state.jdPrefs[PrefKeys.kTabsBarPosition]
if (barPosition == 'top' || barPosition == 'bottom') {
return [
this.createSlider('horizontal', 'unpinnedActiveWidthMax', 'Active Tab Max Width'),
this.createSlider('horizontal', 'unpinnedInactiveWidthMax', 'Inactive Tab Max Width'),
this.createSlider('horizontal', 'pinnedActiveWidthMax', 'Active Pinned Tab Max Width'),
this.createSlider('horizontal', 'pinnedInactiveWidthMax', 'Pinned Tab Max Width'),
React.createElement('div', { className: 'setting-single' },
React.createElement('label', null,
React.createElement('input', {
type: 'checkbox',
checked: tabSize.horizontal.pinnedAllowShrink,
onChange: this.onAllowShrinkChanged.bind(this)
}),
React.createElement('span', null, 'Allow Shrinking of Pinned Tabs')))]
} else {
const ary = [];
[true, false].forEach(isMax => {
const side = isMax ? 'Max' : 'Min'
ary.push(
this.createSlider('vertical', `unpinnedActiveHeight${side}`, `Active Tab ${side} Height`, isMax),
this.createSlider('vertical', `unpinnedInactiveHeight${side}`, `Inactive Tab ${side} Height`, isMax),
this.createSlider('vertical', `pinnedActiveHeight${side}`, `Active Pinned Tab ${side} Height`, isMax),
this.createSlider('vertical', `pinnedInactiveHeight${side}`, `Pinned Tab ${side} Height`, isMax))
})
return ary
}
}
}, { settings: ['TAB_SIZE'], prefs: [PrefKeys.kTabsBarPosition] })
return class extends cls {
render() {
let sup = super.render()
if (sup.props && sup.props.children) {
let section = sup.props.children.find(c =>
c && c.props && c.props.className == 'setting-group tab-min-width')
if (section && section.props && section.props.children) {
let custom = React.createElement(TabWidthSettings, this.props)
section.props.children.push(custom)
}
}
return sup
}
}
})
})()