Skip to content

Commit

Permalink
Fix scrolling
Browse files Browse the repository at this point in the history
  • Loading branch information
Geometrically committed Aug 29, 2024
1 parent d2cdea8 commit 19094ad
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 8 deletions.
12 changes: 11 additions & 1 deletion apps/app-frontend/src/components/ui/PromotionWrapper.vue
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<script setup lang="ts">
<script setup>
import { ref, onMounted, onUnmounted } from 'vue'
import { get as getCreds } from '@/helpers/mr_auth.js'
import { handleError } from '@/store/notifications.js'
import { get_user } from '@/helpers/cache.js'
import { ChevronRightIcon } from '@modrinth/assets'
import { init_ads_window } from '@/helpers/ads.js'
import { listen } from '@tauri-apps/api/event'
const showAd = ref(true)
Expand Down Expand Up @@ -72,6 +73,13 @@ function updateAdPosition() {
}
}
const unlisten = await listen('ads-scroll', (event) => {
if (adsWrapper.value) {
adsWrapper.value.parentNode.scrollTop += event.payload.scroll
updateAdPosition()
}
})
onUnmounted(() => {
if (resizeObserver) {
resizeObserver.disconnect()
Expand All @@ -85,6 +93,8 @@ onUnmounted(() => {
if (scrollHandler) {
window.removeEventListener('scroll', scrollHandler)
}
unlisten()
})
</script>

Expand Down
6 changes: 5 additions & 1 deletion apps/app/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,11 @@ fn main() {
.plugin(
"ads",
InlinedPlugin::new()
.commands(&["init_ads_window", "hide_ads_window"])
.commands(&[
"init_ads_window",
"hide_ads_window",
"scroll_ads_window",
])
.default_permission(
DefaultPermissionRule::AllowAllCommands,
),
Expand Down
3 changes: 2 additions & 1 deletion apps/app/capabilities/ads.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"ads-window"
],
"permissions": [
"shell:allow-open"
"shell:allow-open",
"ads:default"
]
}
2 changes: 1 addition & 1 deletion apps/app/gen/schemas/acl-manifests.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion apps/app/gen/schemas/capabilities.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"ads":{"identifier":"ads","description":"","remote":{"urls":["https://modrinth.com/*","http://localhost:3000/*"]},"local":false,"webviews":["ads-window"],"permissions":["shell:allow-open"]},"core":{"identifier":"core","description":"","local":true,"windows":["main"],"permissions":["core:default","core:path:default","core:event:default","core:window:default","core:app:default","core:resources:default","core:menu:default","core:tray:default","core:window:allow-create","core:window:allow-maximize","core:window:allow-toggle-maximize","core:window:allow-unmaximize","core:window:allow-minimize","core:window:allow-unminimize","core:window:allow-show","core:window:allow-hide","core:window:allow-close","core:window:allow-set-decorations","core:window:allow-start-dragging","core:webview:allow-set-webview-zoom"]},"plugins":{"identifier":"plugins","description":"","local":true,"windows":["main"],"permissions":["dialog:allow-open","dialog:allow-confirm","shell:allow-open","os:allow-platform","os:allow-version","os:allow-os-type","os:allow-family","os:allow-arch","os:allow-exe-extension","os:allow-locale","os:allow-hostname","deep-link:default","window-state:default","window-state:allow-restore-state","window-state:allow-save-window-state","auth:default","import:default","jre:default","logs:default","metadata:default","mr-auth:default","profile-create:default","pack:default","process:default","profile:default","cache:default","settings:default","tags:default","utils:default","ads:default"]}}
{"ads":{"identifier":"ads","description":"","remote":{"urls":["https://modrinth.com/*","http://localhost:3000/*"]},"local":false,"webviews":["ads-window"],"permissions":["shell:allow-open","ads:default"]},"core":{"identifier":"core","description":"","local":true,"windows":["main"],"permissions":["core:default","core:path:default","core:event:default","core:window:default","core:app:default","core:resources:default","core:menu:default","core:tray:default","core:window:allow-create","core:window:allow-maximize","core:window:allow-toggle-maximize","core:window:allow-unmaximize","core:window:allow-minimize","core:window:allow-unminimize","core:window:allow-show","core:window:allow-hide","core:window:allow-close","core:window:allow-set-decorations","core:window:allow-start-dragging","core:webview:allow-set-webview-zoom"]},"plugins":{"identifier":"plugins","description":"","local":true,"windows":["main"],"permissions":["dialog:allow-open","dialog:allow-confirm","shell:allow-open","os:allow-platform","os:allow-version","os:allow-os-type","os:allow-family","os:allow-arch","os:allow-exe-extension","os:allow-locale","os:allow-hostname","deep-link:default","window-state:default","window-state:allow-restore-state","window-state:allow-save-window-state","auth:default","import:default","jre:default","logs:default","metadata:default","mr-auth:default","profile-create:default","pack:default","process:default","profile:default","cache:default","settings:default","tags:default","utils:default","ads:default"]}}
14 changes: 14 additions & 0 deletions apps/app/gen/schemas/desktop-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,13 @@
"ads:allow-init-ads-window"
]
},
{
"description": "ads:allow-scroll-ads-window -> Enables the scroll_ads_window command without any pre-configured scope.",
"type": "string",
"enum": [
"ads:allow-scroll-ads-window"
]
},
{
"description": "ads:deny-hide-ads-window -> Denies the hide_ads_window command without any pre-configured scope.",
"type": "string",
Expand All @@ -334,6 +341,13 @@
"ads:deny-init-ads-window"
]
},
{
"description": "ads:deny-scroll-ads-window -> Denies the scroll_ads_window command without any pre-configured scope.",
"type": "string",
"enum": [
"ads:deny-scroll-ads-window"
]
},
{
"description": "auth:default -> Default plugin permissions.",
"type": "string",
Expand Down
14 changes: 14 additions & 0 deletions apps/app/gen/schemas/macOS-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,13 @@
"ads:allow-init-ads-window"
]
},
{
"description": "ads:allow-scroll-ads-window -> Enables the scroll_ads_window command without any pre-configured scope.",
"type": "string",
"enum": [
"ads:allow-scroll-ads-window"
]
},
{
"description": "ads:deny-hide-ads-window -> Denies the hide_ads_window command without any pre-configured scope.",
"type": "string",
Expand All @@ -334,6 +341,13 @@
"ads:deny-init-ads-window"
]
},
{
"description": "ads:deny-scroll-ads-window -> Denies the scroll_ads_window command without any pre-configured scope.",
"type": "string",
"enum": [
"ads:deny-scroll-ads-window"
]
},
{
"description": "auth:default -> Default plugin permissions.",
"type": "string",
Expand Down
23 changes: 21 additions & 2 deletions apps/app/src/api/ads.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
use serde::Serialize;
use tauri::plugin::TauriPlugin;
use tauri::{LogicalPosition, LogicalSize, Manager, Runtime, WebviewUrl};
use tauri::{
Emitter, LogicalPosition, LogicalSize, Manager, Runtime, WebviewUrl,
};

pub fn init<R: Runtime>() -> TauriPlugin<R> {
tauri::plugin::Builder::<R>::new("ads")
.invoke_handler(tauri::generate_handler![
init_ads_window,
hide_ads_window,
scroll_ads_window,
])
.build()
}
Expand Down Expand Up @@ -33,7 +37,7 @@ pub async fn init_ads_window<R: Runtime>(
tauri::webview::WebviewBuilder::new(
"ads-window",
WebviewUrl::External(
"https://modrinth.com/wrapper/app-ads"
"http://localhost:3000/promo-frame.html"
.parse()
.unwrap(),
),
Expand Down Expand Up @@ -65,3 +69,18 @@ pub async fn hide_ads_window<R: Runtime>(

Ok(())
}

#[derive(Serialize, Clone)]
struct ScrollEvent {
scroll: f32,
}

#[tauri::command]
pub async fn scroll_ads_window<R: Runtime>(
app: tauri::AppHandle<R>,
scroll: f32,
) -> crate::api::Result<()> {
let _ = app.emit("ads-scroll", ScrollEvent { scroll });

Ok(())
}
20 changes: 19 additions & 1 deletion apps/frontend/src/public/promo-frame.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
</head>
<body>
<div class="ads-container">
<a id="plus-link" href="https://modrinth.com/plus"></a>
<a id="plus-link" href="https://modrinth.com/plus" target="_blank"></a>
<div id="modrinth-rail-1" />
</div>
<script>
Expand All @@ -70,6 +70,24 @@
false,
);

window.addEventListener("wheel", (event) => {
if (window.__TAURI_INTERNALS__) {
window.__TAURI_INTERNALS__.invoke("plugin:ads|scroll_ads_window", {
scroll: event.deltaY,
});
}
});

document.getElementById("plus-link").addEventListener("click", (event) => {
event.preventDefault();

if (event.data.modrinthOpenUrl && window.__TAURI_INTERNALS__) {
window.__TAURI_INTERNALS__.invoke("plugin:shell|open", {
path: this.href,
});
}
});

document.addEventListener("contextmenu", (event) => event.preventDefault());
</script>
</body>
Expand Down

0 comments on commit 19094ad

Please sign in to comment.