Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adds basic settings #47

Merged
merged 10 commits into from
Apr 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
PUBLIC_POSTHOG_KEY=""
PUBLIC_POSTHOG_HOST=""
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ dist-ssr
*.ntvs*
*.njsproj
*.sln
*.sw?
*.sw?
.env
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"@tauri-apps/api": ">=2.0.0-beta.0",
"@tauri-apps/plugin-shell": ">=2.0.0-beta.0",
"framer-motion": "^11.0.28",
"posthog-js": "^1.130.0",
"react": "^18.0.0",
"react-dom": "^18.0.0",
"tailwindcss": "^3.0.24"
Expand All @@ -28,4 +29,4 @@
"astro": "^4.3.2",
"typescript": "^5.0.2"
}
}
}
2 changes: 1 addition & 1 deletion src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ scap = { git = "https://github.com/helmerapp/scap/", branch = "main" }
henx = { git = "https://github.com/helmerapp/henx/", branch = "main" }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
tauri-plugin-store = "2.0.0-beta"
tauri-plugin-global-shortcut = "2.0.0-beta"
tauri-plugin-single-instance = "2.0.0-beta"
tauri-plugin-updater = "2.0.0-beta"
tauri-plugin-dialog = "2.0.0-beta"
tauri-plugin-store = "2.0.0-beta"
tokio = { version = "1", features = ["full"] }
window-vibrancy = "0.4.0"
window-shadows = "0.2.1"
Expand Down
5 changes: 3 additions & 2 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,13 @@ fn main() {
})
.manage(AppState::default())
.invoke_handler(tauri::generate_handler![
recorder::request_recording_permission,
recorder::start_recording,
recorder::stop_recording,
recorder::request_recording_permission,
tray::is_ok_sharing_usage_data,
editor::export_handler,
toolbar::show_toolbar,
toolbar::hide_toolbar
toolbar::hide_toolbar,
])
.run(tauri::generate_context!())
.expect("error while running Helmer Micro");
Expand Down
6 changes: 4 additions & 2 deletions src-tauri/src/recorder/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@ pub async fn start_frame_capture(app_handle: AppHandle) {
area[3] as f64 - area[1] as f64,
];

let record_cursor = crate::tray::get_tray_setting(&app_handle, "record_cursor".into());

// Initialize scap
let options = Options {
fps: 60,
targets: Vec::new(),
show_cursor: true,
show_highlight: true,
show_cursor: record_cursor,
show_highlight: false,
excluded_targets: None,
output_type: FRAME_TYPE,
output_resolution: Resolution::_1080p, // TODO: doesn't respect aspect ratio yet
Expand Down
59 changes: 54 additions & 5 deletions src-tauri/src/tray/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@ use crate::cropper::toggle_cropper;
use opener::open;
use tauri::{
image::Image,
menu::{AboutMetadataBuilder, MenuBuilder, MenuItemBuilder, PredefinedMenuItem},
menu::{
AboutMetadataBuilder, CheckMenuItemBuilder, MenuBuilder, MenuItemBuilder,
PredefinedMenuItem,
},
tray::{ClickType, TrayIconBuilder},
AppHandle,
};
use tauri_plugin_store::StoreBuilder;

pub fn build(app: &AppHandle) {
let about_metadata = AboutMetadataBuilder::new()
.short_version("Alpha".into())
.short_version("Beta".into())
.icon(Some(
Image::from_bytes(include_bytes!("../../icons/128x128.png")).expect(""),
))
Expand All @@ -28,10 +32,12 @@ pub fn build(app: &AppHandle) {
.build(app)
.expect(""),
&PredefinedMenuItem::separator(app).expect(""),
&MenuItemBuilder::with_id("show_cursor", "Show Mouse Cursor")
&CheckMenuItemBuilder::with_id("record_cursor", "Record Mouse Cursor")
.checked(get_tray_setting(app, "record_cursor".to_string()))
.build(app)
.expect(""),
&MenuItemBuilder::with_id("start_at_login", "Start at Login")
&CheckMenuItemBuilder::with_id("share_usage_data", "Share Usage Data")
.checked(get_tray_setting(app, "share_usage_data".to_string()))
.build(app)
.expect(""),
&PredefinedMenuItem::separator(app).expect(""),
Expand All @@ -52,7 +58,7 @@ pub fn build(app: &AppHandle) {
.build()
.expect("Failed to build tray menu");

let mut tray = TrayIconBuilder::new()
let mut tray = TrayIconBuilder::with_id("tray")
.menu(&tray_menu)
.icon(Image::from_bytes(include_bytes!("../../icons/128x128.png")).expect(""))
.on_menu_event(move |app, event| match event.id().as_ref() {
Expand All @@ -68,6 +74,8 @@ pub fn build(app: &AppHandle) {
"updates" => {
updater::check_for_update(app.clone()).expect("Failed to check for updates");
}
"record_cursor" => update_tray_setting(app, "record_cursor".to_string()),
"share_usage_data" => update_tray_setting(app, "share_usage_data".to_string()),
_ => (),
})
.on_tray_icon_event(|tray, event| {
Expand All @@ -91,3 +99,44 @@ pub fn build(app: &AppHandle) {

tray.build(app).expect("Failed to build tray");
}

pub fn get_tray_setting(app: &AppHandle, key: String) -> bool {
let mut store = StoreBuilder::new("app_data.bin").build(app.clone());
store.load().unwrap_or_default();

let setting_value = store
.get(key.clone())
.unwrap_or(&serde_json::Value::Bool(true))
.as_bool()
.unwrap();

setting_value
}

fn update_tray_setting(app: &AppHandle, key: String) {
let mut store = StoreBuilder::new("app_data.bin").build(app.clone());
store.load().unwrap_or_default();

// Get current value or true if not found
let setting_value = get_tray_setting(app, key.clone());

match setting_value {
true => {
store.insert(key.clone(), false.into()).unwrap();
}
false => {
store.insert(key.clone(), true.into()).unwrap();
}
}
store.save().expect("Failed to save store");

// log updated value
let updated_value = get_tray_setting(app, key.clone());

println!("{}: {}", key, updated_value);
}

#[tauri::command]
pub async fn is_ok_sharing_usage_data(app: AppHandle) -> bool {
get_tray_setting(&app, "share_usage_data".to_string())
}
69 changes: 53 additions & 16 deletions src/components/editor/index.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,35 @@
import { useState } from 'react';
import { useEffect, useState } from 'react';
import { invoke } from '@tauri-apps/api/core'
import { PostHogProvider, usePostHog } from "posthog-js/react"

import CONSTANTS from '../../constants';
import Preview from "./Preview";
import Controls from "./Controls";
import Trimmer from './Trimmer';

const previewFps = CONSTANTS.previewFps;
const posthogKey = import.meta.env.PUBLIC_POSTHOG_KEY;
const posthogHost = import.meta.env.PUBLIC_POSTHOG_HOST;


export default function Editor() {
const [exporting, setExporting] = useState(false);
const [totalFrames, setTotalFrames] = useState(0);
const [selectedFrames, setSelectedFrames] = useState([0, totalFrames]);
const [isOkSharingUsageData, setIsOkSharingUsageData] = useState(true);

const posthog = usePostHog();

useEffect(() => {
setSelectedFrames([0, totalFrames]);
}, [totalFrames]);

useEffect(() => {
invoke('is_ok_sharing_usage_data').then((res) => {
console.log("Is Ok Sharing Usage Data", res);
setIsOkSharingUsageData(res as boolean);
})
}, [])

const exportHandler = (options: {
fps: number,
Expand All @@ -21,6 +39,17 @@ export default function Editor() {
loop_gif: boolean
}) => {
setExporting(true);

if (isOkSharingUsageData) {
posthog.capture('gif_exported', {
fps: options.fps,
size: options.size,
speed: options.speed,
loop: options.loop_gif,
duration: Math.abs(selectedFrames[1] - selectedFrames[0]) / CONSTANTS.previewFps,
});
}

invoke('export_handler', {
options: {
// Pass the range as time because it may be
Expand All @@ -35,20 +64,28 @@ export default function Editor() {
}

return (
<main className="w-full h-full flex flex-col bg-[#181818] p-8 items-center">
<Preview
onPreviewLoad={(f) => setTotalFrames(f)}
/>
<Trimmer
totalFrames={totalFrames}
selectedFrames={selectedFrames}
setSelectedFrames={setSelectedFrames}
/>
<Controls
exportHandler={exportHandler}
selectedFrames={selectedFrames}
exporting={exporting}
/>
</main>
<PostHogProvider
apiKey={posthogKey}
options={{
api_host: posthogHost,
api_transport: "fetch"
}}
>
<main className="w-full h-full flex flex-col bg-[#181818] p-8 items-center">
<Preview
onPreviewLoad={(f) => setTotalFrames(f)}
/>
<Trimmer
totalFrames={totalFrames}
selectedFrames={selectedFrames}
setSelectedFrames={setSelectedFrames}
/>
<Controls
exporting={exporting}
exportHandler={exportHandler}
selectedFrames={selectedFrames}
/>
</main>
</PostHogProvider>
);
}
18 changes: 18 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1789,6 +1789,11 @@ fastq@^1.6.0:
dependencies:
reusify "^1.0.4"

fflate@^0.4.8:
version "0.4.8"
resolved "https://registry.yarnpkg.com/fflate/-/fflate-0.4.8.tgz#f90b82aefbd8ac174213abb338bd7ef848f0f5ae"
integrity sha512-FJqqoDBR00Mdj9ppamLa/Y7vxm+PRmNWA67N846RvsoYVMKB4q3y/de5PA7gUmRMYK/8CMz2GDZQmCRN1wBcWA==

fill-range@^7.0.1:
version "7.0.1"
resolved "https://registry.yarnpkg.com/fill-range/-/fill-range-7.0.1.tgz#1919a6a7c75fe38b2c7c77e5198535da9acdda40"
Expand Down Expand Up @@ -3121,6 +3126,19 @@ postcss@^8.4.23, postcss@^8.4.28, postcss@^8.4.38:
picocolors "^1.0.0"
source-map-js "^1.2.0"

posthog-js@^1.130.0:
version "1.130.0"
resolved "https://registry.yarnpkg.com/posthog-js/-/posthog-js-1.130.0.tgz#41309b487b05a0bcdff6f0b4f5e2743fe80bfdb0"
integrity sha512-bCrw5HunoXLybO20Q1bYEg68i5WCZWKxhStYJK4OR/9jrm7GwZ53GDrN78p8apFi0EH5ay4YZGbLFSkg+SsZWQ==
dependencies:
fflate "^0.4.8"
preact "^10.19.3"

preact@^10.19.3:
version "10.20.2"
resolved "https://registry.yarnpkg.com/preact/-/preact-10.20.2.tgz#0b343299a8c020562311cc25db93b3d832ec5e71"
integrity sha512-S1d1ernz3KQ+Y2awUxKakpfOg2CEmJmwOP+6igPx6dgr6pgDvenqYviyokWso2rhHvGtTlWWnJDa7RaPbQerTg==

prebuild-install@^7.1.1:
version "7.1.2"
resolved "https://registry.yarnpkg.com/prebuild-install/-/prebuild-install-7.1.2.tgz#a5fd9986f5a6251fbc47e1e5c65de71e68c0a056"
Expand Down