|
| 1 | +<script lang="ts"> |
| 2 | + /* eslint-disable */ |
| 3 | + import { onMount } from "svelte"; |
| 4 | + import SettingsBanner from "./SettingsBanner.svelte"; |
| 5 | + export let root: string; |
| 6 | + export let space_id: string | null; |
| 7 | + export let pwa_enabled: boolean | undefined; |
| 8 | + import { BaseDropdown as Dropdown } from "@gradio/dropdown"; |
| 9 | + import { language_choices, changeLocale } from "../i18n"; |
| 10 | + import { locale, _ } from "svelte-i18n"; |
| 11 | + import { setupi18n } from "../i18n"; |
| 12 | +
|
| 13 | + if (root === "") { |
| 14 | + root = location.protocol + "//" + location.host + location.pathname; |
| 15 | + } |
| 16 | + if (!root.endsWith("/")) { |
| 17 | + root += "/"; |
| 18 | + } |
| 19 | +
|
| 20 | + function setTheme(theme: "light" | "dark" | "system") { |
| 21 | + const url = new URL(window.location.href); |
| 22 | + if (theme === "system") { |
| 23 | + url.searchParams.delete("__theme"); |
| 24 | + current_theme = "system"; |
| 25 | + } else { |
| 26 | + url.searchParams.set("__theme", theme); |
| 27 | + current_theme = theme; |
| 28 | + } |
| 29 | + window.location.href = url.toString(); |
| 30 | + } |
| 31 | +
|
| 32 | + onMount(() => { |
| 33 | + document.body.style.overflow = "hidden"; |
| 34 | + if ("parentIFrame" in window) { |
| 35 | + window.parentIFrame?.scrollTo(0, 0); |
| 36 | + } |
| 37 | + const url = new URL(window.location.href); |
| 38 | + const theme = url.searchParams.get("__theme"); |
| 39 | + current_theme = (theme as "light" | "dark" | "system") || "system"; |
| 40 | + return () => { |
| 41 | + document.body.style.overflow = "auto"; |
| 42 | + }; |
| 43 | + }); |
| 44 | +
|
| 45 | + let current_locale: string; |
| 46 | + let current_theme: "light" | "dark" | "system" = "system"; |
| 47 | +
|
| 48 | + locale.subscribe((value) => { |
| 49 | + if (value) { |
| 50 | + current_locale = value; |
| 51 | + } |
| 52 | + }); |
| 53 | +
|
| 54 | + function handleLanguageChange(e: CustomEvent): void { |
| 55 | + const new_locale = e.detail; |
| 56 | + changeLocale(new_locale); |
| 57 | + } |
| 58 | + setupi18n(); |
| 59 | +</script> |
| 60 | + |
| 61 | +<div class="banner-wrap"> |
| 62 | + <SettingsBanner on:close {root} /> |
| 63 | +</div> |
| 64 | +{#if space_id === null} |
| 65 | + <!-- on Spaces, the theme is set in HF settings --> |
| 66 | + <div class="banner-wrap"> |
| 67 | + <h2>{$_("common.display_theme")}</h2> |
| 68 | + <p class="padded theme-buttons"> |
| 69 | + <li |
| 70 | + class="theme-button {current_theme === 'light' |
| 71 | + ? 'current-theme' |
| 72 | + : 'inactive-theme'}" |
| 73 | + on:click={() => setTheme("light")} |
| 74 | + > |
| 75 | + <button>☀︎ Light</button> |
| 76 | + </li> |
| 77 | + <li |
| 78 | + class="theme-button {current_theme === 'dark' |
| 79 | + ? 'current-theme' |
| 80 | + : 'inactive-theme'}" |
| 81 | + on:click={() => setTheme("dark")} |
| 82 | + > |
| 83 | + <button>⏾ Dark</button> |
| 84 | + </li> |
| 85 | + <li |
| 86 | + class="theme-button {current_theme === 'system' |
| 87 | + ? 'current-theme' |
| 88 | + : 'inactive-theme'}" |
| 89 | + on:click={() => setTheme("system")} |
| 90 | + > |
| 91 | + <button>🖥︎ System</button> |
| 92 | + </li> |
| 93 | + </p> |
| 94 | + </div> |
| 95 | +{/if} |
| 96 | +<div class="banner-wrap"> |
| 97 | + <h2>{$_("common.language")}</h2> |
| 98 | + <p class="padded"> |
| 99 | + <Dropdown |
| 100 | + label="Language" |
| 101 | + choices={language_choices} |
| 102 | + show_label={false} |
| 103 | + {root} |
| 104 | + value={current_locale} |
| 105 | + on:change={handleLanguageChange} |
| 106 | + /> |
| 107 | + </p> |
| 108 | +</div> |
| 109 | +<div class="banner-wrap"> |
| 110 | + <h2>{$_("common.pwa")}</h2> |
| 111 | + <p class="padded"> |
| 112 | + {#if pwa_enabled} |
| 113 | + You can install this app as a Progressive Web App on your device. Visit <a |
| 114 | + href={root}>{root}</a |
| 115 | + > and click the install button in the URL address bar of your browser. |
| 116 | + {:else} |
| 117 | + Progressive Web App is not enabled for this app. To enable it, start your |
| 118 | + Gradio app with <code>launch(pwa=True)</code>. |
| 119 | + {/if} |
| 120 | + </p> |
| 121 | +</div> |
| 122 | + |
| 123 | +<style> |
| 124 | + .banner-wrap { |
| 125 | + position: relative; |
| 126 | + border-bottom: 1px solid var(--border-color-primary); |
| 127 | + padding: var(--size-4) var(--size-6); |
| 128 | + font-size: var(--text-md); |
| 129 | + } |
| 130 | +
|
| 131 | + .banner-wrap h2 { |
| 132 | + font-size: var(--text-xl); |
| 133 | + } |
| 134 | +
|
| 135 | + a { |
| 136 | + text-decoration: underline; |
| 137 | + } |
| 138 | +
|
| 139 | + p.padded { |
| 140 | + padding: 15px 0px; |
| 141 | + } |
| 142 | +
|
| 143 | + .theme-buttons { |
| 144 | + display: flex; |
| 145 | + align-items: center; |
| 146 | + } |
| 147 | +
|
| 148 | + .theme-buttons > * + * { |
| 149 | + margin-left: var(--size-2); |
| 150 | + } |
| 151 | +
|
| 152 | + .theme-button { |
| 153 | + display: flex; |
| 154 | + align-items: center; |
| 155 | + border: 1px solid var(--border-color-primary); |
| 156 | + border-radius: var(--radius-md); |
| 157 | + padding: var(--size-2) var(--size-2-5); |
| 158 | + line-height: 1; |
| 159 | + user-select: none; |
| 160 | + text-transform: capitalize; |
| 161 | + cursor: pointer; |
| 162 | + } |
| 163 | +
|
| 164 | + .current-theme { |
| 165 | + border: 1px solid var(--body-text-color-subdued); |
| 166 | + color: var(--body-text-color); |
| 167 | + } |
| 168 | +
|
| 169 | + .inactive-theme { |
| 170 | + color: var(--body-text-color-subdued); |
| 171 | + } |
| 172 | +
|
| 173 | + .inactive-theme:hover, |
| 174 | + .inactive-theme:focus { |
| 175 | + box-shadow: var(--shadow-drop); |
| 176 | + color: var(--body-text-color); |
| 177 | + } |
| 178 | +
|
| 179 | + .theme-button button { |
| 180 | + all: unset; |
| 181 | + cursor: pointer; |
| 182 | + } |
| 183 | +</style> |
0 commit comments