Skip to content

Commit

Permalink
style: 应用安装二维码支持黑暗模式
Browse files Browse the repository at this point in the history
  • Loading branch information
icyleaf committed Oct 31, 2022
1 parent ae7147d commit b48573c
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 10 deletions.
35 changes: 26 additions & 9 deletions app/controllers/releases/qrcode_controller.rb
Expand Up @@ -3,25 +3,42 @@
class Releases::QrcodeController < ApplicationController
before_action :set_release

LIGHT_BACKGROUND_COLOR = 'FFFFFF'
LIGHT_COLOR = '465960'
THEMES = {
light: {
fill: '#FFFFFF',
color: '#465960'
},
dark: {
fill: '#343a40',
color: '#F0F4Fb'
},
}

##
# 显示应用的二维码
# GET /apps/:slug/(:version)/qrcode
def show
options = {
module_px_size: qrcode_size,
fill: "##{params.fetch(:fill, LIGHT_BACKGROUND_COLOR)}",
color: "##{params.fetch(:color, LIGHT_COLOR)}"
}

render qrcode: friendly_channel_release_url(@release.channel, @release), **options
end

private

def qrcode_size
def options
{
module_px_size: px_size,
fill: theme[:fill],
color: theme[:color]
}
end

def theme
@theme ||= -> do
name = params.fetch(:theme, 'light') == 'light' ? :light : :dark
THEMES[name]
end.call
end

def px_size
case params[:size]
when 'thumb'
3
Expand Down
14 changes: 14 additions & 0 deletions app/helpers/apps_helper.rb
Expand Up @@ -82,4 +82,18 @@ def channel_platform(channel)
def changelog_format(changelog, **options)
simple_format changelog, **options
end

def qrcode_image_tag(release)
logger.debug Setting.site_appearance
logger.debug "Sdfsafasdfasdfsadfd"
if Setting.site_appearance != 'auto'
return image_tag channel_release_qrcode_path(@release.channel, @release, size: :large, theme: Setting.site_appearance)
end

content_tag(:picture) do
content_tag(:source, media: "(prefers-color-scheme: dark)", srcset: channel_release_qrcode_path(release.channel, release, size: :large, theme: :dark)) do
image_tag channel_release_qrcode_path(release.channel, release, size: :large)
end
end
end
end
5 changes: 4 additions & 1 deletion app/views/releases/sidebar/_qrcode.html.slim
Expand Up @@ -8,5 +8,8 @@
i.fas.fa-minus
.card-body.text-center.p-0
.qr_code
= image_tag channel_release_qrcode_path(@release.channel, @release, size: :large)
= qrcode_image_tag(@release)
/ picture
/ source srcset="#{channel_release_qrcode_path(@release.channel, @release, size: :large, theme: :dark)}" media="(prefers-color-scheme: dark)"
/ = image_tag channel_release_qrcode_path(@release.channel, @release, size: :large)
.card-footer.text-center = t('releases.show.scan_qrcode_with_any_suppprted_app')

0 comments on commit b48573c

Please sign in to comment.