Skip to content

Commit

Permalink
feat: add MDYSwitch & replace all Switches with MDYSwitch
Browse files Browse the repository at this point in the history
  • Loading branch information
keiko233 committed Feb 28, 2024
1 parent d8ea303 commit 0c86b80
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 22 deletions.
64 changes: 64 additions & 0 deletions src/components/common/mdy-switch.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { styled } from "@mui/material/styles";
import Switch, { SwitchProps } from "@mui/material/Switch";

const MDYSwitch = styled((props: SwitchProps) => <Switch {...props} />)(
({ theme, checked }) => ({
height: "32px",
padding: 0,
margin: 0,
borderRadius: 24,
"& .MuiSwitch-track": {
borderRadius: 24,
opacity: checked
? "1 !important"
: theme.palette.mode === "dark"
? "0.3 !important"
: "0.7 !important",
backgroundColor: checked
? theme.palette.primary.main
: theme.palette.mode === "dark"
? theme.palette.grey.A700
: theme.palette.grey.A200,
"&::before": {
content: '""',
border: `solid 2px ${theme.palette.grey.A700}`,
width: "100%",
height: "100%",
opacity: checked ? 0 : 1,
position: "absolute",
borderRadius: "inherit",
boxSizing: "border-box",
transitionProperty: "opacity, background-color",
transitionTimingFunction: "linear",
transitionDuration: "67ms",
},
},
"& .MuiSwitch-switchBase": {
padding: "6px 9px 6px 6px",
},
"& .MuiSwitch-thumb": {
boxShadow: "none",
width: 16,
height: 16,
margin: 3,
color: checked
? theme.palette.getContrastText(theme.palette.primary.main)
: theme.palette.mode === "dark"
? theme.palette.grey.A200
: theme.palette.grey.A700,
opacity: checked ? 1 : 0.7,
},
"& .Mui-checked": {
"&.MuiSwitch-switchBase": {
padding: "6px 9px 6px 12px",
},
"& .MuiSwitch-thumb": {
width: 24,
height: 24,
margin: -2,
},
},
}),
);

export default MDYSwitch;
6 changes: 3 additions & 3 deletions src/components/profile/profile-viewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
InputLabel,
MenuItem,
Select,
Switch,
TextField,
styled,
} from "@mui/material";
Expand All @@ -24,6 +23,7 @@ import {
import { Controller, useForm } from "react-hook-form";
import { useTranslation } from "react-i18next";
import { FileInput } from "./file-input";
import MDYSwitch from "../common/mdy-switch";

interface Props {
onChange: () => void;
Expand Down Expand Up @@ -259,7 +259,7 @@ export const ProfileViewer = forwardRef<ProfileViewerRef, Props>(
render={({ field }) => (
<StyledBox>
<InputLabel>{t("Use System Proxy")}</InputLabel>
<Switch checked={field.value} {...field} color="primary" />
<MDYSwitch checked={field.value} {...field} color="primary" />
</StyledBox>
)}
/>
Expand All @@ -270,7 +270,7 @@ export const ProfileViewer = forwardRef<ProfileViewerRef, Props>(
render={({ field }) => (
<StyledBox>
<InputLabel>{t("Use Clash Proxy")}</InputLabel>
<Switch checked={field.value} {...field} color="primary" />
<MDYSwitch checked={field.value} {...field} color="primary" />
</StyledBox>
)}
/>
Expand Down
9 changes: 5 additions & 4 deletions src/components/setting/mods/layout-viewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import { BaseDialog, DialogRef } from "@/components/base";
import { pageTransitionVariants } from "@/components/layout/page-transition";
import { NotificationType, useNotification } from "@/hooks/use-notification";
import { useVerge } from "@/hooks/use-verge";
import { List, MenuItem, Select, Switch } from "@mui/material";
import { List, MenuItem, Select } from "@mui/material";
import { forwardRef, useImperativeHandle, useState } from "react";
import { useTranslation } from "react-i18next";
import { GuardState } from "./guard-state";
import { SettingItem } from "./setting-comp";
import MDYSwitch from "@/components/common/mdy-switch";

export const LayoutViewer = forwardRef<DialogRef>((props, ref) => {
const { t } = useTranslation();
Expand Down Expand Up @@ -51,7 +52,7 @@ export const LayoutViewer = forwardRef<DialogRef>((props, ref) => {
onChange={(e) => onChangeData({ theme_blur: e })}
onGuard={(e) => patchVerge({ theme_blur: e })}
>
<Switch edge="end" />
<MDYSwitch edge="end" />
</GuardState>
</SettingItem>

Expand All @@ -64,7 +65,7 @@ export const LayoutViewer = forwardRef<DialogRef>((props, ref) => {
onChange={(e) => onChangeData({ traffic_graph: e })}
onGuard={(e) => patchVerge({ traffic_graph: e })}
>
<Switch edge="end" />
<MDYSwitch edge="end" />
</GuardState>
</SettingItem>

Expand All @@ -77,7 +78,7 @@ export const LayoutViewer = forwardRef<DialogRef>((props, ref) => {
onChange={(e) => onChangeData({ enable_memory_usage: e })}
onGuard={(e) => patchVerge({ enable_memory_usage: e })}
>
<Switch edge="end" />
<MDYSwitch edge="end" />
</GuardState>
</SettingItem>
{/* TODO: 将 select 单独开一个 Modal 以符合 Material Design 的设计 */}
Expand Down
8 changes: 4 additions & 4 deletions src/components/setting/mods/misc-viewer.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { BaseDialog, DialogRef } from "@/components/base";
import MDYSwitch from "@/components/common/mdy-switch";
import { NotificationType, useNotification } from "@/hooks/use-notification";
import { useVerge } from "@/hooks/use-verge";
import {
Expand All @@ -7,7 +8,6 @@ import {
ListItemText,
MenuItem,
Select,
Switch,
TextField,
} from "@mui/material";
import { useLockFn } from "ahooks";
Expand Down Expand Up @@ -98,7 +98,7 @@ export const MiscViewer = forwardRef<DialogRef>((props, ref) => {

<ListItem sx={{ padding: "5px 2px" }}>
<ListItemText primary={t("Auto Close Connections")} />
<Switch
<MDYSwitch
edge="end"
checked={values.autoCloseConnection}
onChange={(_, c) =>
Expand All @@ -109,7 +109,7 @@ export const MiscViewer = forwardRef<DialogRef>((props, ref) => {

<ListItem sx={{ padding: "5px 2px" }}>
<ListItemText primary={t("Enable Clash Fields Filter")} />
<Switch
<MDYSwitch
edge="end"
checked={values.enableClashFields}
onChange={(_, c) =>
Expand All @@ -120,7 +120,7 @@ export const MiscViewer = forwardRef<DialogRef>((props, ref) => {

<ListItem sx={{ padding: "5px 2px" }}>
<ListItemText primary={t("Enable Builtin Enhanced")} />
<Switch
<MDYSwitch
edge="end"
checked={values.enableBuiltinEnhanced}
onChange={(_, c) =>
Expand Down
4 changes: 2 additions & 2 deletions src/components/setting/mods/sysproxy-viewer.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { BaseDialog, DialogRef } from "@/components/base";
import MDYSwitch from "@/components/common/mdy-switch";
import { NotificationType, useNotification } from "@/hooks/use-notification";
import { useVerge } from "@/hooks/use-verge";
import { getSystemProxy } from "@/services/cmds";
Expand All @@ -8,7 +9,6 @@ import {
List,
ListItem,
ListItemText,
Switch,
TextField,
Typography,
styled,
Expand Down Expand Up @@ -101,7 +101,7 @@ export const SysproxyViewer = forwardRef<DialogRef>((props, ref) => {
<List>
<ListItem sx={{ padding: "5px 2px" }}>
<ListItemText primary={t("Proxy Guard")} />
<Switch
<MDYSwitch
edge="end"
disabled={!enabled}
checked={value.guard}
Expand Down
6 changes: 3 additions & 3 deletions src/components/setting/setting-clash.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
IconButton,
MenuItem,
Select,
Switch,
TextField,
Tooltip,
Typography,
Expand All @@ -23,6 +22,7 @@ import { ControllerViewer } from "./mods/controller-viewer";
import { GuardState } from "./mods/guard-state";
import { SettingItem, SettingList } from "./mods/setting-comp";
import { WebUIViewer } from "./mods/web-ui-viewer";
import MDYSwitch from "../common/mdy-switch";

const isWIN = getSystem() === "windows";

Expand Down Expand Up @@ -71,7 +71,7 @@ const SettingClash = ({ onError }: Props) => {
onChange={(e) => onChangeData({ "allow-lan": e })}
onGuard={(e) => patchClash({ "allow-lan": e })}
>
<Switch edge="end" />
<MDYSwitch edge="end" />
</GuardState>
</SettingItem>

Expand All @@ -84,7 +84,7 @@ const SettingClash = ({ onError }: Props) => {
onChange={(e) => onChangeData({ ipv6: e })}
onGuard={(e) => patchClash({ ipv6: e })}
>
<Switch edge="end" />
<MDYSwitch edge="end" />
</GuardState>
</SettingItem>

Expand Down
13 changes: 7 additions & 6 deletions src/components/setting/setting-system.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import useSWR from "swr";
import { useRef } from "react";
import { useTranslation } from "react-i18next";
import { IconButton, Switch } from "@mui/material";
import { IconButton } from "@mui/material";
import { ArrowForward, PrivacyTipRounded, Settings } from "@mui/icons-material";
import { checkService } from "@/services/cmds";
import { useVerge } from "@/hooks/use-verge";
Expand All @@ -11,6 +11,7 @@ import { GuardState } from "./mods/guard-state";
import { ServiceViewer } from "./mods/service-viewer";
import { SysproxyViewer } from "./mods/sysproxy-viewer";
import getSystem from "@/utils/get-system";
import MDYSwitch from "../common/mdy-switch";

interface Props {
onError?: (err: Error) => void;
Expand Down Expand Up @@ -66,7 +67,7 @@ const SettingSystem = ({ onError }: Props) => {
onChange={(e) => onChangeData({ enable_tun_mode: e })}
onGuard={(e) => patchVerge({ enable_tun_mode: e })}
>
<Switch edge="end" />
<MDYSwitch edge="end" />
</GuardState>
</SettingItem>

Expand Down Expand Up @@ -94,7 +95,7 @@ const SettingSystem = ({ onError }: Props) => {
onChange={(e) => onChangeData({ enable_service_mode: e })}
onGuard={(e) => patchVerge({ enable_service_mode: e })}
>
<Switch
<MDYSwitch
edge="end"
disabled={
serviceStatus !== "active" && serviceStatus !== "installed"
Expand Down Expand Up @@ -127,7 +128,7 @@ const SettingSystem = ({ onError }: Props) => {
onChange={(e) => onChangeData({ enable_system_proxy: e })}
onGuard={(e) => patchVerge({ enable_system_proxy: e })}
>
<Switch edge="end" />
<MDYSwitch edge="end" />
</GuardState>
</SettingItem>

Expand All @@ -140,7 +141,7 @@ const SettingSystem = ({ onError }: Props) => {
onChange={(e) => onChangeData({ enable_auto_launch: e })}
onGuard={(e) => patchVerge({ enable_auto_launch: e })}
>
<Switch edge="end" />
<MDYSwitch edge="end" />
</GuardState>
</SettingItem>

Expand All @@ -153,7 +154,7 @@ const SettingSystem = ({ onError }: Props) => {
onChange={(e) => onChangeData({ enable_silent_start: e })}
onGuard={(e) => patchVerge({ enable_silent_start: e })}
>
<Switch edge="end" />
<MDYSwitch edge="end" />
</GuardState>
</SettingItem>
</SettingList>
Expand Down

0 comments on commit 0c86b80

Please sign in to comment.