chiark / gitweb /
wdt: Break out posg2posw, nfc
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Tue, 26 Jan 2021 23:02:39 +0000 (23:02 +0000)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Tue, 26 Jan 2021 23:02:39 +0000 (23:02 +0000)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
wdriver.rs

index a0858d23a065319755c8d89c959cd7f40e5daa29..35dbae41d224e2e2a44c420468872049d9372933 100644 (file)
@@ -774,43 +774,11 @@ impl<'g> WindowGuard<'g> {
       w: self,
     }
   }
-}
-
-pub type WebCoord = i32;
-pub type WebPos = (WebCoord, WebCoord);
-
-pub struct PieceElement<'g> {
-  pieceid: &'g str,
-  w: &'g WindowGuard<'g>,
-  elem: t4::WebElement<'g>,
-}
-
-impl<'g> Deref for PieceElement<'g> {
-  type Target = t4::WebElement<'g>;
-  fn deref<'i>(&'i self) -> &'i t4::WebElement<'g> { &self.elem }
-}
-
-impl<'g> PieceElement<'g> {
-  #[throws(AE)]
-  pub fn posg(&self) -> Pos {
-    (||{
-      let a = |a| Ok::<_,AE>(
-        self.get_attribute(a)?.ok_or(anyhow!("{}", a))?.parse()?
-      );
-      let x = a("x")?;
-      let y = a("y")?;
-      Ok::<_,AE>(PosC([x,y]))
-    })()
-      .with_context(|| self.pieceid.to_owned())
-      .context("read position of piece out of x,y attributes")?
-  }
 
   #[throws(AE)]
-  pub fn posw(&self) -> WebPos {
-    let posg = self.posg()?;
-
-    let mat = self.w.matrix.get_or_try_init(||{
-      let ary = self.w.su.driver.execute_script(r#"
+  pub fn posg2posw(&'g self, posg: Pos) -> WebPos {
+    let mat = self.matrix.get_or_try_init(||{
+      let ary = self.su.driver.execute_script(r#"
         let m = space.getScreenCTM();
         return [m.a, m.b, m.c, m.d, m.e, m.f];
       "#)?;
@@ -857,9 +825,46 @@ impl<'g> PieceElement<'g> {
         coord()?,
         coord()?,
       ))
+    })()
+      .context("convert game position to web page coordinates")?
+  }
+}
+
+pub type WebCoord = i32;
+pub type WebPos = (WebCoord, WebCoord);
+
+pub struct PieceElement<'g> {
+  pieceid: &'g str,
+  w: &'g WindowGuard<'g>,
+  elem: t4::WebElement<'g>,
+}
+
+impl<'g> Deref for PieceElement<'g> {
+  type Target = t4::WebElement<'g>;
+  fn deref<'i>(&'i self) -> &'i t4::WebElement<'g> { &self.elem }
+}
+
+impl<'g> PieceElement<'g> {
+  #[throws(AE)]
+  pub fn posg(&self) -> Pos {
+    (||{
+      let a = |a| Ok::<_,AE>(
+        self.get_attribute(a)?.ok_or(anyhow!("{}", a))?.parse()?
+      );
+      let x = a("x")?;
+      let y = a("y")?;
+      Ok::<_,AE>(PosC([x,y]))
     })()
       .with_context(|| self.pieceid.to_owned())
-      .context("find piece position")?
+      .context("read position of piece out of x,y attributes")?
+  }
+
+  #[throws(AE)]
+  pub fn posw(&self) -> WebPos {
+    let posg = self.posg()?;
+
+    self.w.posg2posw(posg)
+      .with_context(|| self.pieceid.to_owned())?
   }
 }