Skip to content

Commit

Permalink
Merge pull request #1295 from h3poteto/iss-1187/another-win
Browse files Browse the repository at this point in the history
refs #1187 Allow to open media in another window
  • Loading branch information
h3poteto authored Jan 2, 2024
2 parents 58ec13f + 70c0e79 commit 93f272d
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 5 deletions.
1 change: 1 addition & 0 deletions src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ url = "2.5"
simplelog = "0.12"
once_cell = "1.19.0"
rust-i18n = "2"
base64 = "0.21"

[features]
# by default Tauri runs in production mode
Expand Down
2 changes: 2 additions & 0 deletions src-tauri/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ app_menu:
help: Help
window: Window
crash_reporting: Crash Report
media:
title: Media
22 changes: 19 additions & 3 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
all(not(debug_assertions), target_os = "windows"),
windows_subsystem = "windows"
)]
use std::{fs::OpenOptions, path::PathBuf, str::FromStr, sync::Arc};

use base64::{engine::general_purpose, Engine};
use megalodon::{self, oauth};
use rust_i18n::t;
use serde::Serialize;
use std::{fs::OpenOptions, path::PathBuf, str::FromStr, sync::Arc};
use tauri::{async_runtime::Mutex, AppHandle, Manager, State};

mod database;
mod entities;
mod favicon;
Expand Down Expand Up @@ -463,6 +463,21 @@ async fn frontend_log(message: String, level: String) -> () {
()
}

#[tauri::command]
async fn open_media(app_handle: AppHandle, media_url: String) -> () {
let encoded = general_purpose::STANDARD_NO_PAD.encode(media_url.clone());

let _ = tauri::WindowBuilder::new(
&app_handle,
encoded,
tauri::WindowUrl::External(media_url.parse().unwrap()),
)
.menu(menu::media_menu())
.title(t!("media.title"))
.build()
.expect("failed to build media window");
}

async fn start_timeline_streaming(
app_handle: Arc<AppHandle>,
sqlite_pool: &sqlx::SqlitePool,
Expand Down Expand Up @@ -646,6 +661,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
switch_devtools,
frontend_log,
update_column_width,
open_media,
])
.setup(|app| {
let app_handle = app.handle();
Expand Down
6 changes: 6 additions & 0 deletions src-tauri/src/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,9 @@ pub fn menu() -> Menu {
}
menu
}

pub fn media_menu() -> Menu {
let submenu = Submenu::new("File", Menu::new().add_native_item(MenuItem::CloseWindow));
let menu = Menu::new().add_submenu(submenu);
menu
}
8 changes: 7 additions & 1 deletion src/components/Media.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Entity } from 'megalodon'
import { ReactElement, useCallback, useEffect, useState } from 'react'
import { Icon } from '@rsuite/icons'
import { BsChevronRight, BsChevronLeft } from 'react-icons/bs'
import { invoke } from '@tauri-apps/api/tauri'

type Props = {
index: number
Expand Down Expand Up @@ -88,6 +89,10 @@ const Media: React.FC<Props> = props => {
}

const mediaComponent = (media: Entity.Attachment): ReactElement => {
const externalWindow = async (url: string) => {
await invoke('open_media', { mediaUrl: url })
}

switch (media.type) {
case 'gifv':
return (
Expand All @@ -109,7 +114,8 @@ const mediaComponent = (media: Entity.Attachment): ReactElement => {
fill
alt={media.description ? media.description : media.id}
title={media.description ? media.description : media.id}
style={{ objectFit: 'contain' }}
style={{ objectFit: 'contain', cursor: 'pointer' }}
onClick={() => externalWindow(media.url)}
/>
)
}
Expand Down
14 changes: 13 additions & 1 deletion src/components/timelines/status/Attachments.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import Image from 'next/image'
import { Entity } from 'megalodon'
import { Button, IconButton } from 'rsuite'
import { Icon } from '@rsuite/icons'
import { BsEyeSlash, BsCaretRightFill, BsVolumeUp } from 'react-icons/bs'
import { BsEyeSlash, BsCaretRightFill, BsVolumeUp, BsBoxArrowUpRight } from 'react-icons/bs'
import { useEffect, useState } from 'react'
import { FormattedMessage } from 'react-intl'
import emptyPreview from 'src/black.png'
import { ColumnWidth } from 'src/entities/timeline'
import { invoke } from '@tauri-apps/api/tauri'

type Props = {
attachments: Array<Entity.Attachment>
Expand Down Expand Up @@ -123,6 +124,10 @@ type AttachmentProps = {
const Attachment: React.FC<AttachmentProps> = props => {
const { media, changeSensitive } = props

const externalWindow = async (url: string) => {
await invoke('open_media', { mediaUrl: url })
}

return (
<div style={{ position: 'relative' }}>
<IconButton
Expand All @@ -132,6 +137,13 @@ const Attachment: React.FC<AttachmentProps> = props => {
onClick={changeSensitive}
style={{ position: 'absolute', top: '4px', left: '4px' }}
/>
<IconButton
icon={<Icon as={BsBoxArrowUpRight} />}
size="sm"
appearance="subtle"
onClick={() => externalWindow(media.url)}
style={{ position: 'absolute', top: '4px', right: '4px' }}
/>
{(media.type === 'gifv' || media.type === 'video') && (
<IconButton
icon={<Icon as={BsCaretRightFill} />}
Expand Down

0 comments on commit 93f272d

Please sign in to comment.