Skip to content

Commit 2afcc13

Browse files
committed
Add widget to convey the amount of sync storage in use
1 parent ba833d3 commit 2afcc13

File tree

6 files changed

+97
-29
lines changed

6 files changed

+97
-29
lines changed

platform/chromium/vapi-background.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1649,6 +1649,28 @@ vAPI.cloud = (( ) => {
16491649
return entry;
16501650
};
16511651

1652+
const used = async function(dataKey) {
1653+
if ( webext.storage.sync.getBytesInUse instanceof Function === false ) {
1654+
return;
1655+
}
1656+
const coarseCount = await getCoarseChunkCount(dataKey);
1657+
if ( typeof coarseCount !== 'number' ) { return; }
1658+
const keys = [];
1659+
for ( let i = 0; i < coarseCount; i++ ) {
1660+
keys.push(`${dataKey}${i}`);
1661+
}
1662+
let results;
1663+
try {
1664+
results = await Promise.all([
1665+
webext.storage.sync.getBytesInUse(keys),
1666+
webext.storage.sync.getBytesInUse(null),
1667+
]);
1668+
} catch(ex) {
1669+
}
1670+
if ( Array.isArray(results) === false ) { return; }
1671+
return { used: results[0], total: results[1], max: QUOTA_BYTES };
1672+
};
1673+
16521674
const getOptions = function(callback) {
16531675
if ( typeof callback !== 'function' ) { return; }
16541676
callback(options);
@@ -1665,7 +1687,7 @@ vAPI.cloud = (( ) => {
16651687
getOptions(callback);
16661688
};
16671689

1668-
return { push, pull, getOptions, setOptions };
1690+
return { push, pull, used, getOptions, setOptions };
16691691
})();
16701692

16711693
/******************************************************************************/

src/cloud-ui.html

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,18 @@
66
</head>
77
<body>
88
<div id="cloudToolbar">
9-
<button id="cloudPush" type="button" data-i18n-title="cloudPush"><span class="fa-icon">cloud-upload</span></button>
10-
<span id="cloudInfo" data-i18n="cloudNoData"></span>
11-
<button id="cloudPull" type="button" data-i18n-title="cloudPull" disabled><span class="fa-icon">cloud-download</span></button>
12-
<button id="cloudPullAndMerge" type="button" data-i18n-title="cloudPullAndMerge" disabled><span class="fa-icon">cloud-download</span><span class="fa-icon">plus</span></button>
13-
<span id="cloudCog" class="fa-icon">cog</span>
9+
<div>
10+
<button id="cloudPush" type="button" data-i18n-title="cloudPush"><span class="fa-icon">cloud-upload</span></button>
11+
<span id="cloudInfo" data-i18n="cloudNoData"></span>
12+
<button id="cloudPull" type="button" data-i18n-title="cloudPull" disabled><span class="fa-icon">cloud-download</span></button>
13+
<button id="cloudPullAndMerge" type="button" data-i18n-title="cloudPullAndMerge" disabled><span class="fa-icon">cloud-download</span><span class="fa-icon">plus</span></button>
14+
</div>
15+
<div id="cloudCog" class="fa-icon">cog</div>
1416
<div id="cloudOptions">
15-
<label data-i18n="cloudDeviceNamePrompt"></label> <input id="cloudDeviceName" type="text" value=""> <button id="cloudOptionsSubmit" class="vflex" type="button" data-i18n="genericSubmit"></button>
17+
<label data-i18n="cloudDeviceNamePrompt"></label> <input id="cloudDeviceName" type="text" value=""> <button id="cloudOptionsSubmit" type="button" data-i18n="genericSubmit"></button>
1618
</div>
1719
</div>
1820
<div id="cloudError"></div>
21+
<div id="cloudCapacity"><div><div></div></div></div>
1922
</body>
2023
</html>

src/css/cloud-ui.css

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,29 @@
11
#cloudWidget {
22
background: url("../img/cloud.png") hsl(216, 100%, 93%);
3-
border-radius: 3px;
43
margin: 0.5em 0;
54
overflow: auto;
6-
padding: 0.5em;
75
position: relative;
86
}
97
#cloudWidget.hide {
108
display: none;
119
}
12-
#cloudToolbar > button {
13-
font-size: 180%;
14-
padding: 0 0.25em;
15-
position: relative;
10+
#cloudWidget div {
11+
display: flex;
1612
}
1713
#cloudToolbar {
18-
display: flex;
14+
align-items: flex-start;
1915
flex-wrap: nowrap;
16+
justify-content: space-between;
17+
}
18+
#cloudToolbar > div:first-of-type {
19+
margin: 0.5em;
20+
}
21+
#cloudToolbar button {
22+
padding: 0 0.25em;
23+
position: relative;
24+
}
25+
#cloudToolbar button .fa-icon {
26+
font-size: 180%;
2027
}
2128
#cloudToolbar button[disabled] {
2229
visibility: hidden;
@@ -28,56 +35,65 @@
2835
margin-left: 0.25em;
2936
}
3037
#cloudPullAndMerge > span:nth-of-type(2) {
31-
font-size: 50%;
38+
font-size: 90%;
3239
position: absolute;
3340
right: 0;
3441
top: 0;
3542
}
3643
#cloudInfo {
37-
color: var(--fg-0-60);
3844
flex-shrink: 0;
3945
font-size: 90%;
4046
margin: 0 1em;
4147
overflow: hidden;
4248
padding: 0;
4349
white-space: pre-line;
4450
}
51+
#cloudCapacity {
52+
background-color: var(--light-gray-30);
53+
height: 4px;
54+
}
55+
#cloudCapacity > div {
56+
background-color: var(--light-gray-60);
57+
}
58+
#cloudCapacity > div > div {
59+
background-color: var(--violet-60);
60+
}
4561
#cloudError {
4662
color: var(--fg-icon-info-lvl-4);
4763
flex-grow: 1;
4864
flex-shrink: 2;
4965
font-size: small;
50-
margin: 0.5em 0.5em 0 0;
66+
margin: 0 0.5em 0.5em 0.5em;
5167
}
5268
#cloudError:empty {
5369
display: none;
5470
}
55-
#cloudToolbar #cloudCog {
71+
#cloudCog {
5672
color: var(--fg-0-50);
5773
cursor: pointer;
5874
fill: var(--fg-0-50);
59-
flex-grow: 1;
6075
font-size: 110%;
6176
justify-content: flex-end;
6277
padding: 0.4em;
6378
}
64-
#cloudToolbar #cloudCog:hover {
79+
#cloudCog:hover {
6580
color: inherit;
6681
fill: inherit;
6782
}
68-
#cloudToolbar #cloudOptions {
83+
#cloudWidget #cloudOptions {
6984
background-color: var(--default-surface);
7085
border: 1px solid var(--bg-1-border);
71-
bottom: 0;
7286
display: none;
73-
margin: 0.4em;
74-
padding: 0.4em;
87+
padding: 0.5em;
7588
position: absolute;
7689
right: 0;
7790
text-align: center;
78-
top: 0;
7991
z-index: 10;
8092
}
81-
#cloudToolbar #cloudOptions.show {
93+
#cloudWidget #cloudOptions.show {
8294
display: block;
95+
white-space: nowrap;
96+
}
97+
#cloudOptions button {
98+
min-height: var(--default-gap-xlarge);
8399
}

src/css/themes/default.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
--light-gray-90: #80808f;
3939
--red-60: #e22850;
4040
--violet-40: #ab71ff;
41+
--violet-60: #7542e5;
4142
--violet-70: #592acb;
4243
--violet-80: #45278d;
4344
--yellow-30: #ffd567;

src/js/cloud-ui.js

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ self.cloud = {
3434
datakey: '',
3535
data: undefined,
3636
onPush: null,
37-
onPull: null
37+
onPull: null,
3838
};
3939

4040
/******************************************************************************/
@@ -47,6 +47,25 @@ if ( self.cloud.datakey === '' ) { return; }
4747

4848
/******************************************************************************/
4949

50+
const fetchStorageUsed = async function() {
51+
const elem = widget.querySelector('#cloudCapacity');
52+
if ( elem.classList.contains('hide') ) { return; }
53+
const result = await vAPI.messaging.send('cloudWidget', {
54+
what: 'cloudUsed',
55+
datakey: self.cloud.datakey,
56+
});
57+
if ( result instanceof Object === false ) {
58+
elem.classList.add('hide');
59+
return;
60+
}
61+
const total = (result.total / result.max * 100).toFixed(1);
62+
elem.firstElementChild.style.width = `${total}%`;
63+
const used = (result.used / result.total * 100).toFixed(1);
64+
elem.firstElementChild.firstElementChild.style.width = `${used}%`;
65+
};
66+
67+
/******************************************************************************/
68+
5069
const fetchCloudData = async function() {
5170
const entry = await vAPI.messaging.send('cloudWidget', {
5271
what: 'cloudPull',
@@ -94,6 +113,7 @@ const pushData = async function() {
94113
.textContent = failed ? error : '';
95114
if ( failed ) { return; }
96115
fetchCloudData();
116+
fetchStorageUsed();
97117
};
98118

99119
/******************************************************************************/
@@ -151,7 +171,7 @@ const submitOptions = async function() {
151171

152172
const onInitialize = function(options) {
153173
if ( options instanceof Object === false ) { return; }
154-
if ( !options.enabled ) { return; }
174+
if ( options.enabled !== true ) { return; }
155175
self.cloud.options = options;
156176

157177
const xhr = new XMLHttpRequest();
@@ -180,12 +200,13 @@ const onInitialize = function(options) {
180200
uDom('#cloudCog').on('click', openOptions);
181201
uDom('#cloudOptions').on('click', closeOptions);
182202
uDom('#cloudOptionsSubmit').on('click', ( ) => { submitOptions(); });
183-
203+
184204
fetchCloudData().then(result => {
185205
if ( typeof result !== 'string' ) { return; }
186206
document.getElementById('cloudPush').classList.add('error');
187207
document.querySelector('#cloudError').textContent = result;
188208
});
209+
fetchStorageUsed();
189210
};
190211
xhr.send();
191212
};

src/js/messaging.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -828,6 +828,11 @@ const onMessage = function(request, sender, callback) {
828828
callback(result);
829829
});
830830

831+
case 'cloudUsed':
832+
return vAPI.cloud.used(request.datakey).then(result => {
833+
callback(result);
834+
});
835+
831836
default:
832837
break;
833838
}

0 commit comments

Comments
 (0)