Skip to content

Commit

Permalink
Pwa for ios
Browse files Browse the repository at this point in the history
  • Loading branch information
HeHang0 committed Nov 9, 2023
1 parent 7b19302 commit d908e21
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 21 deletions.
5 changes: 5 additions & 0 deletions web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta
name="apple-mobile-web-app-status-bar-style"
content="black-translucent" />
<meta name="format-detection" content="telephone=no" />
<title>音乐和</title>
</head>
<body>
Expand Down
5 changes: 1 addition & 4 deletions web/scripts/fix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,7 @@ function transformIndexHtmlHandler(html: string) {
prependedHtml += '\n ' + scriptPwa;
html = html.replace(matchScript, '');
}
return html.replace(
'<head>',
'<head>' + prependedHtml.replaceAll('="./', '="/musiche/')
);
return html.replace('<head>', '<head>' + prependedHtml);
}

export const FixPwaPlugin = function () {
Expand Down
29 changes: 16 additions & 13 deletions web/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Footer from './components/Footer.vue';
import CurrentList from './components/CurrentList.vue';
import PlayDetail from './components/PlayDetail.vue';
import WindowHelper from './components/WindowHelper.vue';
import { webView2Services } from './utils/utils';
import { isIOS, isInStandaloneMode, webView2Services } from './utils/utils';
import { MusicConnection } from './stores/connection';
import { usePlayStore } from './stores/play';
import { useSettingStore } from './stores/setting';
Expand All @@ -29,13 +29,16 @@ iconLink.rel = 'icon';
iconLink.href = LogoCircleImage;
document.head.appendChild(iconLink);
new MusicConnection(webView2Services.enabled);
let rootClass = webView2Services.enabled ? 'webview-host' : '';
if (isInStandaloneMode) rootClass += ' standalone';
if (isIOS) rootClass += ' ios';
</script>

<template>
<el-container
class="music-layout"
direction="vertical"
:class="webView2Services.enabled ? 'webview-host' : ''"
:class="rootClass"
:style="
setting.appTheme.image
? `background: url(${setting.appTheme.image}) 50% 50% / cover`
Expand Down Expand Up @@ -74,11 +77,7 @@ new MusicConnection(webView2Services.enabled);
}
&-top {
overflow: hidden;
flex: unset;
height: calc(100% - 80px);
&-full {
height: 100%;
}
flex: 1;
}
&-top {
width: 100%;
Expand All @@ -87,12 +86,16 @@ new MusicConnection(webView2Services.enabled);
.music-main {
position: relative;
overflow-x: hidden;
:deep(.el-scrollbar > .el-scrollbar__wrap > .el-scrollbar__view) {
position: relative;
min-height: 100%;
}
}
</style>

<style lang="less">
.music-main > .el-scrollbar > .el-scrollbar__wrap > .el-scrollbar__view {
position: relative;
min-height: 100%;
@media (max-width: 720px) {
.music-layout.standalone.ios {
.music-footer {
padding-bottom: 10px;
}
}
}
</style>
2 changes: 0 additions & 2 deletions web/src/components/Footer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,6 @@ const play = usePlayStore();
opacity: 0.8;
}
.music-footer {
position: fixed;
bottom: 0;
background-color: var(--music-footer-background);
height: 80px;
width: 100vw;
Expand Down
10 changes: 9 additions & 1 deletion web/src/stores/setting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ const controlKeys = [
'metaright'
];
const darkModeMediaQuery = window.matchMedia('(prefers-color-scheme: dark)');
const appleMobileWebAppStatusBarStyle: HTMLMetaElement | null =
document.querySelector('meta[name="apple-mobile-web-app-status-bar-style"]');
export const useSettingStore = defineStore('setting', {
state: () => ({
maximized: false,
Expand Down Expand Up @@ -242,8 +244,9 @@ export const useSettingStore = defineStore('setting', {
},
setAppTheme(appTheme?: AppTheme) {
let preferredColorScheme = 0;
const dark = appTheme?.id.includes('dark');
if (!this.autoAppTheme) {
preferredColorScheme = appTheme?.id.includes('dark') ? 2 : 1;
preferredColorScheme = dark ? 2 : 1;
}
this.appTheme.id = appTheme?.id || '';
this.appTheme.color = appTheme?.color || '';
Expand All @@ -254,6 +257,11 @@ export const useSettingStore = defineStore('setting', {
id: this.appTheme.id,
name: this.appTheme.name
});
if (appleMobileWebAppStatusBarStyle) {
appleMobileWebAppStatusBarStyle.content = dark
? 'black-translucent'
: 'default';
}
musicOperate('/theme?theme=' + preferredColorScheme);
},
setCustomTheme(appTheme?: AppTheme) {
Expand Down
7 changes: 7 additions & 0 deletions web/src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ try {
webView2Services.enabled = !!webView2Services.specialService;
} catch (error) {}

export const isInStandaloneMode = Boolean(
'standalone' in window.navigator && window.navigator.standalone
);
export const isIOS = !!navigator.userAgent.match(
/\(i[^;]+;( U;)? CPU.+Mac OS X/
);

export function scrollToElementId(
id: string,
center?: boolean,
Expand Down
3 changes: 2 additions & 1 deletion web/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ const plugins = [
FixJSMediaTagsErrorPlugin(),
VitePWA({
manifest: {
id: 'top.picapico.musiche',
name: 'Musiche',
short_name: 'Musiche',
description: '音乐播放器[支持网易云、QQ音乐、咪咕音乐].',
start_url: 'musiche',
start_url: '',
scope: '.',
display: 'standalone',
background_color: '#ffffff',
Expand Down

0 comments on commit d908e21

Please sign in to comment.