@@ -43,6 +43,9 @@ pub struct UpstreamSnapshot {
4343 pub history_index : usize ,
4444 pub animating : bool ,
4545 pub last_error : Option < String > ,
46+ pub scroll_pos : ( f32 , f32 ) ,
47+ pub content_size : ( f32 , f32 ) ,
48+ pub viewport_size : ( f32 , f32 ) ,
4649}
4750
4851#[ derive( Debug , Clone ) ]
@@ -61,6 +64,7 @@ pub struct ServoUpstreamConfig {
6164 pub pixel_format : PixelFormat ,
6265 pub alpha_mode : AlphaMode ,
6366 pub color_space : ColorSpace ,
67+ pub scale_factor : f32 ,
6468 pub enable_pixel_probe : bool ,
6569 pub resources_dir : Option < PathBuf > ,
6670 pub certificate_path : Option < PathBuf > ,
@@ -88,6 +92,9 @@ impl Default for UpstreamSnapshot {
8892 history_index : 0 ,
8993 animating : false ,
9094 last_error : None ,
95+ scroll_pos : ( 0.0 , 0.0 ) ,
96+ content_size : ( 0.0 , 0.0 ) ,
97+ viewport_size : ( 0.0 , 0.0 ) ,
9198 }
9299 }
93100}
@@ -319,6 +326,7 @@ pub struct ServoUpstreamRuntime {
319326 resource_source : ResourceDirSource ,
320327 pending_clipboard_request : Arc < std:: sync:: Mutex < Option < StringRequest > > > ,
321328 event_sender : std:: sync:: mpsc:: Sender < EngineEvent > ,
329+ scale_factor : f32 ,
322330}
323331
324332impl std:: fmt:: Debug for ServoUpstreamRuntime {
@@ -437,6 +445,7 @@ impl ServoUpstreamRuntime {
437445 pending_clipboard_request. clone ( ) ,
438446 ) ) ;
439447 let webview = WebViewBuilder :: new ( & servo, rendering_context. clone ( ) )
448+ . hidpi_scale_factor ( libservo:: Scale :: new ( config. scale_factor ) )
440449 . delegate ( delegate)
441450 . clipboard_delegate ( clipboard_delegate)
442451 . build ( ) ;
@@ -480,6 +489,7 @@ impl ServoUpstreamRuntime {
480489 resource_source : source,
481490 pending_clipboard_request,
482491 event_sender,
492+ scale_factor : config. scale_factor ,
483493 } )
484494 }
485495
@@ -511,6 +521,7 @@ impl ServoUpstreamRuntime {
511521 }
512522
513523 pub fn select_all ( & self ) {
524+ tracing:: info!( target: "brazen::servo" , "UPSTREAM: select_all executed" ) ;
514525 let script = "document.execCommand('selectAll', false, null);" . to_string ( ) ;
515526 let webview_id = self . webview . id ( ) ;
516527 self . servo . javascript_evaluator_mut ( ) . evaluate (
@@ -520,6 +531,67 @@ impl ServoUpstreamRuntime {
520531 ) ;
521532 }
522533
534+ pub fn copy ( & self ) {
535+ tracing:: info!( target: "brazen::servo" , "UPSTREAM: copy executed" ) ;
536+ let script = "document.execCommand('copy', false, null);" . to_string ( ) ;
537+ let webview_id = self . webview . id ( ) ;
538+ self . servo . javascript_evaluator_mut ( ) . evaluate (
539+ webview_id,
540+ script,
541+ Box :: new ( |_| { } ) ,
542+ ) ;
543+ }
544+
545+ pub fn paste ( & self ) {
546+ tracing:: info!( target: "brazen::servo" , "UPSTREAM: paste executed" ) ;
547+ let script = "document.execCommand('paste', false, null);" . to_string ( ) ;
548+ let webview_id = self . webview . id ( ) ;
549+ self . servo . javascript_evaluator_mut ( ) . evaluate (
550+ webview_id,
551+ script,
552+ Box :: new ( |_| { } ) ,
553+ ) ;
554+ }
555+
556+ pub fn update_scroll_info ( & self ) {
557+ let script = "JSON.stringify({
558+ scrollX: window.scrollX,
559+ scrollY: window.scrollY,
560+ contentWidth: document.documentElement.scrollWidth,
561+ contentHeight: document.documentElement.scrollHeight,
562+ viewportWidth: window.innerWidth,
563+ viewportHeight: window.innerHeight
564+ })" . to_string ( ) ;
565+
566+ let snapshot = self . snapshot . clone ( ) ;
567+ let webview_id = self . webview . id ( ) ;
568+ self . servo . javascript_evaluator_mut ( ) . evaluate (
569+ webview_id,
570+ script,
571+ Box :: new ( move |res| {
572+ if let Ok ( val) = res {
573+ if let libservo:: JSValue :: String ( s) = val {
574+ if let Ok ( json) = serde_json:: from_str :: < serde_json:: Value > ( & s) {
575+ let mut snap = snapshot. borrow_mut ( ) ;
576+ snap. scroll_pos = (
577+ json[ "scrollX" ] . as_f64 ( ) . unwrap_or ( 0.0 ) as f32 ,
578+ json[ "scrollY" ] . as_f64 ( ) . unwrap_or ( 0.0 ) as f32
579+ ) ;
580+ snap. content_size = (
581+ json[ "contentWidth" ] . as_f64 ( ) . unwrap_or ( 0.0 ) as f32 ,
582+ json[ "contentHeight" ] . as_f64 ( ) . unwrap_or ( 0.0 ) as f32
583+ ) ;
584+ snap. viewport_size = (
585+ json[ "viewportWidth" ] . as_f64 ( ) . unwrap_or ( 0.0 ) as f32 ,
586+ json[ "viewportHeight" ] . as_f64 ( ) . unwrap_or ( 0.0 ) as f32
587+ ) ;
588+ }
589+ }
590+ }
591+ } ) ,
592+ ) ;
593+ }
594+
523595 pub fn navigate ( & self , url : & str ) -> Result < ( ) , String > {
524596 let url = Url :: parse ( url) . map_err ( |error| format ! ( "invalid url: {error}" ) ) ?;
525597 self . webview . load ( url) ;
@@ -620,8 +692,9 @@ impl ServoUpstreamRuntime {
620692 }
621693
622694 pub fn handle_mouse_move ( & self , x : f32 , y : f32 ) {
695+ let scale = self . scale_factor ;
623696 self . handle_input ( InputEvent :: MouseMove ( MouseMoveEvent :: new (
624- WebViewPoint :: Device ( DevicePoint :: new ( x, y) ) ,
697+ WebViewPoint :: Device ( DevicePoint :: new ( x * scale , y * scale ) ) ,
625698 ) ) ) ;
626699 }
627700
@@ -631,10 +704,11 @@ impl ServoUpstreamRuntime {
631704 } else {
632705 MouseButtonAction :: Up
633706 } ;
707+ let scale = self . scale_factor ;
634708 self . handle_input ( InputEvent :: MouseButton ( MouseButtonEvent :: new (
635709 action,
636710 MouseButton :: from ( button) ,
637- WebViewPoint :: Device ( DevicePoint :: new ( x, y) ) ,
711+ WebViewPoint :: Device ( DevicePoint :: new ( x * scale , y * scale ) ) ,
638712 ) ) ) ;
639713 }
640714
@@ -651,9 +725,10 @@ impl ServoUpstreamRuntime {
651725 z : 0.0 ,
652726 mode : WheelMode :: DeltaPixel ,
653727 } ;
728+ let scale = self . scale_factor ;
654729 self . handle_input ( InputEvent :: Wheel ( WheelEvent :: new (
655730 delta,
656- WebViewPoint :: Device ( DevicePoint :: new ( x, y) ) ,
731+ WebViewPoint :: Device ( DevicePoint :: new ( x * scale , y * scale ) ) ,
657732 ) ) ) ;
658733 }
659734
0 commit comments